{ } public class Type1 { } publi...">

C # Reflection: how to use an "object" as a "parameter type",

I have this domain:

public class GenClass<T> { }

public class Type1 { }

public class Type2 { }

public class GenType1 : GenClass<Type1> { }

public class GenType2 : GenClass<Type1> { }

public class GenType3 : GenClass<Type2> { }

public class GenType4 : GenClass<string> { } 

public class GenType5 : GenClass<int> { } 

and this logic:

public class SomeClass {
    public void Add<T>(GenClass<T> t) { }
}

and this consumer:

public class Consumer {
    public void Method() {
        var some = new SomeClass();
        some.Add(new GenType1());
        some.Add(new GenType2());
        some.Add(new GenType3());
        some.Add(new GenType4());
        some.Add(new GenType5());
    }
}

But instead of adding each GenType(n)separately, I create a method like this:

public void AddFromAssembly<TAssembly>(SomeClass some, Type baseType) {
    var list = typeof(TAssembly).Assembly.GetTypes()
        .Where(t => baseType.IsAssignableFrom(t)
            && !baseType.Equals(t))
        .ToList();
    list.ForEach(type => {
        var ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null,
                                       Type.EmptyTypes, null);
        var obj = ctor.Invoke(new object[] { });
        some.Add(obj); //????
    });
}

and want to call it like this:

public class Consumer {
    public void Method() {
        var some = new SomeClass();
        AddFromAssembly<GenType1>(some, typeof(GenClass<>));
    }
}

But the method some.Addis a general method, and the method ctor.Invokereturns object. Do you have an idea to solve this problem? Thanks in advance.

UPDATE The question was incomplete, I am correcting it. Thanks for the review.

+1
source share
2 answers

Or do the following:

Add<object>(obj); //this will work with T typed as object

or

typeof(Some).GetMethod("Add").MakeGenericMethod(new [] { typeof(obj.GetType()).Invoke(some, new [] { obj });

The reflection version will use the exact type obj for T at runtime.

+7

- ( , ).

, , , , "", .

+1

All Articles