createinstance

CreateInstance of a Type in another AppDomain

倖福魔咒の 提交于 2019-12-07 13:00:54
问题 My scenario is that I have a .net application (let's say a Console App) that creates AppDomains. It then needs to create instances and call methods on Types that are in that AppDomain. Each AppDomain has a specific directory where are it's dependecies should be, which is not under (or even near) the Console Apps directory. Here's my simple code: string baseDirectory = "c:\foo"; // <- where AppDomain's dependecies // set up the app domain AppDomainSetup setup = new AppDomainSetup(); setup

Is it possible to create an instance of an class without running ANY code from the class? (no ctor, no field initializations)

自作多情 提交于 2019-12-07 03:54:06
问题 I have created an engine that takes in 3rd party plugins (DLL's) that implement an interface. Since I have no control over the code that gets plugged in, I want to be able to run 1 specific method (from the interface) from the correct class (GetTypes loop untill I find the interfaced class ). Since anyone can create nice constructor code that executes on Activator.CreateInstance , I can solve that by using FormatterServices.GetUninitializedObject . But that does not help when code is being

System.Activator.CreateInstance returning null

徘徊边缘 提交于 2019-12-06 02:31:20
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 there information from these. for(int at = 0; at < numberOfAttacks; ++at ) { attmono[at] = (MonoScript)

How can I pass an argument to a C# plug-in being loaded through Assembly.CreateInstance?

Deadly 提交于 2019-12-05 11:39:34
What I have now (which successfully loads the plug-in) is this: Assembly myDLL = Assembly.LoadFrom("my.dll"); IMyClass myPluginObject = myDLL.CreateInstance("MyCorp.IMyClass") as IMyClass; This only works for a class that has a constructor with no arguments. How do I pass in an argument to a constructor? You cannot. Instead use Activator.CreateInstance as shown in the example below (note that the Client namespace is in one DLL and the Host in another. Both must be found in the same directory for code to work.) However, if you want to create a truly pluggable interface, I suggest you use an

Is it possible to create an instance of an class without running ANY code from the class? (no ctor, no field initializations)

眉间皱痕 提交于 2019-12-05 07:17:25
I have created an engine that takes in 3rd party plugins (DLL's) that implement an interface. Since I have no control over the code that gets plugged in, I want to be able to run 1 specific method (from the interface) from the correct class (GetTypes loop untill I find the interfaced class ). Since anyone can create nice constructor code that executes on Activator.CreateInstance , I can solve that by using FormatterServices.GetUninitializedObject . But that does not help when code is being initialized on fields in the class. public class myclass : myinterface { public someotherclass name = new

How to pass ctor args in Activator.CreateInstance or use IL?

偶尔善良 提交于 2019-11-28 03:43:34
I need a performance enhanced Activator.CreateInstance() and came across this article by Miron Abramson that uses a factory to create the instance in IL and then cache it. (I've included code below from Miron Abramson's site in case it somehow disappears). I'm new to IL Emit code and anything beyond Activator.CreateInstance() for instantiating a class and any help would be much appreciative. My problem is that I need to create an instance of an object that takes a ctor with a parameter. I see there is a way to pass in the Type of the parameter, but is there a way to pass in the value of the

How to pass ctor args in Activator.CreateInstance or use IL?

雨燕双飞 提交于 2019-11-27 05:15:35
问题 I need a performance enhanced Activator.CreateInstance() and came across this article by Miron Abramson that uses a factory to create the instance in IL and then cache it. (I've included code below from Miron Abramson's site in case it somehow disappears). I'm new to IL Emit code and anything beyond Activator.CreateInstance() for instantiating a class and any help would be much appreciative. My problem is that I need to create an instance of an object that takes a ctor with a parameter. I see

How to Pass Parameters to Activator.CreateInstance<T>()

耗尽温柔 提交于 2019-11-27 00:43:03
I want to create an instance of a type that I specify in a generic method that I have. This type has a number of overloaded constructors. I'd like to be able to pass arguments to the constructors, but Activator.CreateInstance<T>() doesn't see to have this as an option. Is there another way to do it? Yes. (T)Activator.CreateInstance(typeof(T), param1, param2); sudhAnsu63 There is another way to pass arguments to CreateInstance through named parameters. Based on that, you can pass a array towards CreateInstance . This will allow you to have 0 or multiple arguments. public T CreateInstance<T>

Fast creation of objects instead of Activator.CreateInstance(type)

不问归期 提交于 2019-11-26 18:42:07
I'm trying to improve the performance of our application. We have a lot of Activator.CreateInstance calls that are causing some grief. We instantiate a lot of classes based on an interface (ITabDocument) and after looking around I thought of using this code: The code is no better (infact marginally slower) than using the Activator.CreateInstance code we had. public static Func<T> CreateInstance<T>(Type objType) where T : class, new() { var dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + objType.Name, objType, null, objType); ILGenerator ilGen = dynMethod.GetILGenerator(); ilGen.Emit(OpCodes

How to Pass Parameters to Activator.CreateInstance<T>()

大城市里の小女人 提交于 2019-11-26 09:25:11
问题 I want to create an instance of a type that I specify in a generic method that I have. This type has a number of overloaded constructors. I\'d like to be able to pass arguments to the constructors, but Activator.CreateInstance<T>() doesn\'t see to have this as an option. Is there another way to do it? 回答1: Yes. (T)Activator.CreateInstance(typeof(T), param1, param2); 回答2: There is another way to pass arguments to CreateInstance through named parameters. Based on that, you can pass a array