Unlike C ++ templates, where you can "template templates" or "partial specialization" , C # general arguments can only go to the depth of the announcement site, and general restrictions can only tell you about the origin and nothing more. If you want to be able to refer to the common argument of one of your common arguments, the only way to do this, as in your example, is by using the general restriction on the hereditary line ( T : BaseClass<V> ), and then V must also be identified in general signature. You need something like below.
public class Factory{ public static T Get<T, V>(V v) where T : BaseClass<V> { T someObject = DIContainer.Resolve<T>(); someObject.Set(v); } }
All I added here is a generic V argument for your method signature. Again, if you did not have a base class for binding V to, you would not be able to do much in your situation. For example, if the runtime type of T itself was general and not its base class, you would be stuck, as in the example below, which does not compile.
public class Factory{ public static T Get<T, V>(V v) where T : T<V> { T someObject = DIContainer.Resolve<T>(); someObject.Set(v); } }
source share