activator

What is the advantage of UnityContainer.Resolve over Activator.CreateInstance?

折月煮酒 提交于 2019-12-22 05:53:08
问题 I'm just getting started with Unity. I am still wondering what its advantages are. UnityContainer().Resolve<T>() which can return a concrete instance of the type that is registered for the generic type T . I think I can also use Activator.CreateInstance<T>() . Activator is a built-in class in the .NET Framework, so I am wondering what the distinct feature for Unity is? 回答1: MSDN states these as the advantages of Unity Container: Simplified object creation, especially for hierarchical object

Activator.CreateInstance can't find the constructor (MissingMethodException)

老子叫甜甜 提交于 2019-12-21 06:58:10
问题 I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent(); compositeObject = CompositeObject; } along with a default constructor with no parameters. Next I'm trying to create an instance, but it only works without parameters: var designer = Activator.CreateInstance(designerAttribute.Designer); This works just fine, but if I want to pass parameters it does not: var designer = Activator.CreateInstance

InvalidCastException of a Activator.CreateInstance object during an installation procedure

爷,独闯天下 提交于 2019-12-20 04:06:25
问题 I have the following procedure private static IMyInterface OpenInstance( string assemblyPath, string classType, string assemblyName, out AppDomain domainInstall) { IMyInterface interface = null; AppDomainSetup domaininfo = new AppDomainSetup(); domaininfo.ApplicationBase = assemblyPath; domainInstall = AppDomain.CreateDomain("PathInstall", null, domaininfo); ObjectHandle handleService = null; try { handleService = Activator.CreateInstance( domainInstall, assemblyName, classType, true, System

InvalidCastException of a Activator.CreateInstance object during an installation procedure

送分小仙女□ 提交于 2019-12-20 04:05:26
问题 I have the following procedure private static IMyInterface OpenInstance( string assemblyPath, string classType, string assemblyName, out AppDomain domainInstall) { IMyInterface interface = null; AppDomainSetup domaininfo = new AppDomainSetup(); domaininfo.ApplicationBase = assemblyPath; domainInstall = AppDomain.CreateDomain("PathInstall", null, domaininfo); ObjectHandle handleService = null; try { handleService = Activator.CreateInstance( domainInstall, assemblyName, classType, true, System

Activator and static classes

﹥>﹥吖頭↗ 提交于 2019-12-19 05:31:11
问题 I'm tossing around the idea of using the Activator class in order to get access to resources in an assembly that I would otherwise create a circular reference for (dependency injection). I've done it before with vanilla classes that I needed a reference to, but my question is: can I use the Activator to get access to a static class? The part that's tripping me up is that the Activator returns to you a instance of the object, whereas a static class has no instance. Is this possible? 回答1: You

Set property Nullable<> by reflection

混江龙づ霸主 提交于 2019-12-19 05:31:05
问题 I try to set a Nullable<> property dynamicly. I Get my property ex : PropertyInfo property = class.GetProperty("PropertyName"); // My property is Nullable<> at this time So the type could be a string or int I want to set my property by reflection like property.SetValue(class,"1256",null); It's not working when my property is a Nullable<> Generic. So i try to find a way to set my property. To know the type of my nullable<> property i execute Nullable.GetUnderlyingType(property.PropertyType)

cast with a Type variable

此生再无相见时 提交于 2019-12-14 02:16:02
问题 The below code won't work, I wanted to know how I can dynamically cast an instance to a type determined at runtime? Convert.ChangeType() returns an Object that still needs to be cast. So does all attempts to Invoke() a GetConstructor() or Activator.CreateInstance(), see below. At some point I need to explicitly cast in code, I'm hoping to avoid it or push it out as far as possible. Type type = Type.GetType ("RandomChildClass"); Object obj = Activator.CreateInstance (type, new Object[]{

System.Activator.CreateInstance returning null

孤街醉人 提交于 2019-12-12 09:48:44
问题 The problem I have is that CreateInstance returns null. Here is the code: if(spattmono[0] != null) { if((SpecialAttack) System.Activator.CreateInstance( spattmono[0].GetClass()) == null) { Debug.Log("DUMB ACTIVATOR!!!"); } //combo.SetSpecialAttack(spattack); } Attack and SpecialAttack are both classes that store basic information, and inherit from UnityEngine.Object . Attmono and spattmono are both MonoScript arrays, attmono being able to hold 16 and spattmono being able to hold 4. They get

How to dynamically create generic C# object using reflection? [duplicate]

两盒软妹~` 提交于 2019-12-11 19:44:05
问题 This question already has answers here : Pass An Instantiated System.Type as a Type Parameter for a Generic Class (6 answers) Closed 5 years ago . In C# I have the following object: public class Item { } public class Task<T> { } public class TaskA<T> : Task<T> { } public class TaskB<T> : Task<T> { } I want to dynamically create TaskA or TaskB using C# reflection ( Activator.CreateInstance ). However I wouldn't know the type before hand, so I need to dynamically create TaskA based on string

How can I instantiate a COM class interface generically

这一生的挚爱 提交于 2019-12-11 18:17:52
问题 I'm trying to refactor a piece of code and ran out of options I can think off. This is the original code I had: if (WebConfigSettings.ComPartition == null && HttpContext.Current != null) Nses = new NSession(); else Nses = (INSession)Marshal.BindToMoniker(string.Format("partition:{0}/new:NuntioServer.NSession", WebConfigSettings.ComPartition)); AND if (WebConfigSettings.ComPartition == null && HttpContext.Current != null) apses.Wses = new WSession(); else apses.Wses = (IWSession)Marshal