简体   繁体   English

Activator.CreateInstance需要另一个对象进行实例化

[英]Activator.CreateInstance needs another object to be instantiated

I am instantiating my objects with the classical 我用经典实例化我的对象

object myObject = Activator.CreateInstance(myType);

code and if works fine. 代码,如果工作正常。 The thing is that, now, Id like to instantiate an object and in its constructor, there is a reference to another object. 问题是,现在,我喜欢实例化一个对象,并且在它的构造函数中,有一个对另一个对象的引用。 So if I just do the code above, I got a NullReferenceException exception : 所以,如果我只是执行上面的代码,我得到一个NullReferenceException异常:

Object reference not set to an instance of an object.

I tried to instantiate the concerned object (with Activator.CreateInstance) but I got the same exception... I feel like instantiating the problematic object before my 2nd CreateInstance call is not enough. 我尝试实例化相关对象(使用Activator.CreateInstance)但我得到了相同的异常...我觉得在我的第二个CreateInstance调用不够之前实例化有问题的对象。 What should I do ? 我该怎么办 ?

EDIT : here's the code of the problem 编辑:这是问题的代码

//A regionManager in needed by MainView as far as I understand the Exception's details
var regionManager = Assembly.LoadFrom("RegionView.dll");
Type rmType = regionManager.GetType("Framework.Hmi.RegionManager");
object obj = Activator.CreateInstance(rmType);
//This works !

var shellViewLibrary = Assembly.LoadFrom("ShellView.dll");
Type svType = shellViewLibrary.GetType("Framework.ShellView.MainView");
object objjj = Activator.CreateInstance(svType);

The last line fails and the error is a NullReferenceException with the details : 最后一行失败,错误是NullReferenceException其中包含详细信息:

at Hmi.RegionManager.get_RegionFactory()
at Hmi.RegionManager.CreateRegion(DependencyObject element)
at Hmi.RegionManager.OnSetRegionNameCallback(DependencyObject element, DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
[...]

I call c/c the 30/40 other error lines but I don't think it is that usefull... 我把c / c称为30/40其他错误行,但我不认为它有用...

When you say your constructor references another object, do you mean you should be passing it to the constructor as a parameter? 当你说你的构造函数引用另一个对象时,你的意思是你应该将它作为参数传递给构造函数吗? If so, then there is an overload for Activator.CreateInstance that allows you to specify the parameter values. 如果是这样,那么Activator.CreateInstance会出现重载,允许您指定参数值。 See MSDN for details, but basically, you add them after the type. 有关详细信息,请参阅MSDN ,但基本上,您可以在类型后添加它们。

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

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