C#反射:如何使用“对象”作为“类型参数”

编程入门 行业动态 更新时间:2024-10-28 18:22:29
本文介绍了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 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#反射:如何使用“对象”作为“类型参数”

本文发布于:2023-07-30 00:49:49,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   反射   对象   参数   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!