Also see this answer from @Jon Skeet - and, in particular, a quote about IsAssignableFrom.
I do not think that this is possible the way you imagined it.
I would suggest you put an “front-end contract” on your Foo classes, and then let the generics do their work.
. - - , ...
class Factory
{
public static T Create<T, TVal>(TVal obj) where T : class, IFoo<TVal>, new()
{
return new T { Value = obj };
}
}
interface IFoo<TVal>
{
TVal Value { get; set; }
}
class Foo : IFoo<string>
{
public string Value { get; set; }
public Foo() { }
}
public T Get<T, TVal>(TVal obj) where T : class, IFoo<TVal>, new()
{
return Factory.Create<T, TVal>(obj);
}
- , have that luxury - ..
( )
Foo foo = Get<Foo, string>("another text");