C#反射:如何使用“对象”作为“类型参数” [英] C# Reflection: How to use an "object" as a "type-parameter"

查看:181
本文介绍了C#反射:如何使用“对象”作为“类型参数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个域:

  public class GenClass< T> {} 

public class Type1 {}

public class Type2 {}

public class GenType1:GenClass< Type1> {}

public class GenType2:GenClass< Type1> {}

公共类GenType3:GenClass< Type2> {}

public class GenType4:GenClass< string> {}

public class GenType5:GenClass< int> {}

以及此逻辑:

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

以及此消费者:

  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());


$ / code>

但不是每个 GenType(n)分开,我创建一个像这样的方法:

  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); // ????
});
}

并且想要像这样调用它:

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


$ / code>

但是 some.Add 方法是一个通用方法,并且 ctor.Invoke 方法返回一个对象
您有任何想法来解决这个问题吗?提前致谢。



更新问题不完整,我解决了。感谢您的评论。

解决方案

要么做到这一点:

 添加<对象>(OBJ); //这可以用T类型作为对象

或者:

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

反射版本将在运行时为T使用obj的确切类型。 p>

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 some.Add method is a generic method and ctor.Invoke method returns an object. Have you any idea to solve this problem? Thanks in advance.

UPDATE The question was incomplete, I fix it. Thanks for review.

解决方案

Either do this:

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 of obj for T at runtime.

这篇关于C#反射:如何使用“对象”作为“类型参数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆