简体   繁体   English

如何为各种构造函数做Activator.CreateInstance?

[英]How to do Activator.CreateInstance for various constructors?

I need to get my subclasses of DataContext and I found the below function for that lying around somewhere and it sure finds my subclass but I can't instantiate it :( 我需要获取DataContext的子类,并且在下面的某个地方找到了下面的函数,它肯定找到了我的子类,但是我无法实例化它:(

  public static IEnumerable<T> GetSubclassesFor<T>(Assembly assembly)
  {
   return (assembly.GetTypes()
    .Where(t => t.BaseType == (typeof (T)))
    .Select(t => (T) Activator.CreateInstance(t, new object[] {"asdasd"})))
    .ToList();
  }

I get the following error message: 我收到以下错误消息:

System.Reflection.TargetInvocationException : Ett undantagsfel har inträffat i målet för en aktivering. System.Reflection.TargetInvocationException:创建活动时发生的事件。 ----> System.TypeInitializationException : Typinitieraren för PlaynGO.Cashier.Data.CashierDC utlöste ett undantag. ----> System.TypeInitializationException:从PlaynGO.Cashier.Data.CashierDC utty标签到Typinitieraren。 ----> System.NullReferenceException : Objektreferensen har inte angetts till en instans av ett objekt. ----> System.NullReferenceException:对象获取到对象为止。 vid System.RuntimeMethodHandle. vid System.RuntimeMethodHandle。 InvokeConstructor(IRuntimeMethodInfo method, Object[] args, ref SignatureStruct signature, RuntimeType declaringType) vid System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) vid System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) vid System.Activator.CreateInstance(Type type, Object[] args) vid PlaynGO.Dbml.Reflexion.b _3(Type t) i Reflexion.cs: line 23 vid System.Linq.Enumerable.WhereSelectArrayIterator 2.MoveNext() vid System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) vid System.Linq.Enumerable.ToList(IEnumerable 1 source) vid PlaynGO.Dbml.Reflexion.GetInstances(Assembly assembly) i Reflexion.cs: line 23 vid PlaynGO.Dbml.UnitTests.TestReflection.TestGettingTypes() i TestReflection.cs: line 21 --TypeInitializationException vid PlaynGO.Cashier.Data.CashierDC..ctor(String connection) --NullReferenceExc InvokeConstructor(IRuntimeMethodInfo方法,Object []参数,ref SignatureStruct签名,RuntimeType声明类型)vid System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr,活页夹活页夹,Object []参数,CultureInfo文化)vid System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr活页夹活页夹,Object []参数,CultureInfo文化,Object [] activationAttributes)vid System.Activator.CreateInstance(Type type,Object [] args)vid PlaynGO.Dbml.Reflexion.b _3(Type t)i Reflexion.cs:line 23 vid System.Linq.Enumerable.WhereSelectArrayIterator 2.MoveNext() vid System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) vid System.Linq.Enumerable.ToList(IEnumerable 1 source)vid PlaynGO.Dbml.Reflexion .GetInstances(装配体)i Reflexion.cs:第23行vid PlaynGO.Dbml.UnitTests.TestReflection.TestGettingTypes()i TestReflection.cs:第21行--TypeInitializationException vid PlaynGO.Cashier.Data.CashierDC..ctor(字符串连接) --NullReferenceExc eption vid PlaynGO.Cashier.Data.CashierDC..cctor() eption vid PlaynGO.Cashier.Data.CashierDC..cctor()

The constructor I want to call is the following: 我要调用的构造函数如下:

    public CashierDC(string connection) :
   base(connection, mappingSource)

MappingSource is directly instantiated and is an instance field. MappingSource是直接实例化的,并且是一个实例字段。 Where do I go wrong? 我哪里出错了? What do I have to do to make this work? 我需要做些什么来完成这项工作?

PS. PS。 This is .NET 4.0 这是.NET 4.0

I think your error is something else. 我认为您的错误是另外一回事。 If Activator.CreateInstance can't find your constructor you get a MissingMethodException not a TargetInvocationException . 如果Activator.CreateInstance找不到您的构造函数,则会得到MissingMethodException而不是TargetInvocationException I suspect there's something else wrong in the actual class you're trying to instantiate. 我怀疑您要实例化的实际类中还有其他问题。

The TargetInvocationException indicates that the constructor that it is invoking has thrown an exception. TargetInvocationException表示它正在调用的构造函数引发了异常。 Perhaps this is due to the actual value that you are passing in for the connection string. 也许这是由于您为连接字符串传递的实际值。 You might want to try using the debugger and set a break point in the constructor taking a connection string and (1) make sure that it is being invoked and (2) determine where the exception is occurring. 您可能想尝试使用调试器,并在构造函数中使用连接字符串设置断点,并且(1)确保正在调用它,并且(2)确定发生异常的位置。

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

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