在 c# 中使用 Type.GetType() 返回的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/914578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:04:46  来源:igfitidea点击:

using type returned by Type.GetType() in c#

c#genericsreflection

提问by

i've got a question about how is it possible (if possible :) to use a type reference returned by Type.GetType() to, for example, create IList of that type?

我有一个问题,关于如何(如果可能:) 使用 Type.GetType() 返回的类型引用来创建该类型的 IList?

here's sample code :

这是示例代码:

Type customer = Type.GetType("myapp.Customer");
IList<customer> customerList = new List<customer>(); // got an error here =[

Thank You in Advance !

先感谢您 !

采纳答案by Stefan Steinegger

Something like this:

像这样的东西:

Type listType = typeof(List<>).MakeGenericType(customer);
IList customerList = (IList)Activator.CreateInstance(listType);

Of course you can't declare it as

当然你不能把它声明为

IList<customer>

because it is not defined at compile time. But List<T>implements IList, so you can use this.

因为它不是在编译时定义的。但是List<T>实现了 IList,所以你可以使用它。

回答by Ruben

The solution to your problem is provided by Stefan already.

Stefan 已经提供了您问题的解决方案。

The reason that you can not do IList<customer>is because you can not mix compile time and run-time types this way. A hint when I try to reason about something like this is: how can intellisense figure out what members it must show. In your example this can only be resolved at runtime.

你不能这样做的原因IList<customer>是因为你不能以这种方式混合编译时和运行时类型。当我试图对这样的事情进行推理时,一个提示是:智能感知如何确定它必须显示哪些成员。在您的示例中,这只能在运行时解决。

The answer given by Stefan, can be used. However I think it does not help in your underlying problem, because it does not give you intellisense. So I think you have no advantage over using just a non-generic list.

可以使用 Stefan 给出的答案。但是我认为它对您的潜在问题没有帮助,因为它没有给您智能感知。因此,我认为与仅使用非通用列表相比,您没有任何优势。

回答by Xavier Guzman

I recently faced this problem..I realize this is an old question, but thought someone might find useful the solution i found (using net 4.0 ). This is the setup i had:

我最近遇到了这个问题..我意识到这是一个老问题,但认为有人可能会发现我找到的解决方案有用(使用 net 4.0 )。这是我的设置:

public interface ISomeInterface{
     String GetEntityType{ get; }
}

public abstract class BaseClass : ISomeInterface{
     public String GetEntityType { 
          get{ return this.GetType().ToString(); }
     }
}

public class ChildA : BaseClass {  }

public class ChildB : BaseClass {  }

What I did was create a factory method:

我所做的是创建一个工厂方法:

public static dynamic GetRealType { ISomeInterface param } {
     return Activator.CreateInstance("dllname", param.GetEntityType);
}

Creating the object as a dynamic type was what solved it for me. I inferred the type by having a method:

将对象创建为动态类型为我解决了这个问题。我通过一种方法推断出类型:

public void DoSomething<T>(T param){

     IList<T> list = somecode...
}

this allowed me to do a call to the method and let .net infer the type

这允许我调用该方法并让 .net 推断类型

public void myMethod( ISomeInterface param ) {
     dynamic value = Factory.GetRealType ( param );
     DoSomething(value);
}

Hope i didn't make it all confusing and it actually helps, also, sorry for the wall of text

希望我没有让这一切变得混乱,它实际上有帮助,也很抱歉文字墙