简体   繁体   English

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

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

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 ! 先感谢您 !

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,所以你可以使用它。

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 ). 我最近遇到了这个问题。我发现这是一个老问题,但是我觉得有人可能会发现我找到的解决方案很有用(使用net 4.0)。 This is the setup i had: 这是我的设置:

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 希望我没有让它变得令人困惑,它实际上也有助于对文本的墙壁感到抱歉

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. 您无法执行IList<customer>的原因是您不能以这种方式混合编译时和运行时类型。 A hint when I try to reason about something like this is: how can intellisense figure out what members it must show. 当我尝试推理类似这样的事情时的暗示是:intellisense如何确定它必须显示的成员。 In your example this can only be resolved at runtime. 在您的示例中,这只能在运行时解决。

The answer given by Stefan, can be used. 可以使用Stefan给出的答案。 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. 所以我认为你只使用非通用列表没有优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM