activator

Use Activator.CreateInstance() and support multiple constructor signatures

烈酒焚心 提交于 2021-02-11 18:22:59
问题 I need to instantiate a type that MUST have a IConfiguration parameter, but MAY have other parameters. Currently I use the following code: Activator.CreateInstance(myType, BindingFlags.CreateInstance, null, new object[] { configuration }, CultureInfo.InvariantCulture); myType is the type to instantiate. Written like this, it requires a constructor that has exactly one parameter of type IConfiguration . As you can see, I pass the configuration object via object array to satisfy this

Activator.GetObject for .netstandard

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-29 05:42:42
问题 everybody! I have a piece of code which calls win service remotely with Activator.GetObject(type, url) . But the project is targeting .net framework and we're going to migrate to .net standard. The later, though, does not provide such behavior. Is there a way to do the same action in .net standard? 来源: https://stackoverflow.com/questions/56150153/activator-getobject-for-netstandard

Can I use Activator.CreateInstance with an Interface?

我只是一个虾纸丫 提交于 2020-01-29 06:15:54
问题 I have an example: Assembly asm = Assembly.Load("ClassLibrary1"); Type ob = asm.GetType("ClassLibrary1.UserControl1"); UserControl uc = (UserControl)Activator.CreateInstance(ob); grd.Children.Add(uc); There I'm creating an instance of a class, but how can I create an instance of a class which implements some interface? i.e. UserControl1 implements ILoad interface. U: I can cast object to interface later, but I don't know which type in the assemblies implements the interface. 回答1: This is some

How to create the class instances dynamically and call the implementation

亡梦爱人 提交于 2020-01-25 08:02:45
问题 I have various interfaces and I need to be able to call them. Here is the base class: public class MyActorBase<TChild>: ActorBase where TChild : MyActorBase<TChild> { public MyActorBase() { var actors = ChildClass .GetInterfaces() .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IActorMessageHandler<>)) .Select(x=> (arguments: x.GetGenericArguments(), definition: x)) .ToImmutableList(); if (actors.Any()) { var ty = actors.First(); var obj = Activator.CreateInstance(ty

C# Activator createInstance for extending class

笑着哭i 提交于 2020-01-16 00:58:07
问题 I have a base class, which is as follows: public Data() { id = num++; SetVariables(); } //fill every Variable varNames, parseInduction, noise, seperator in Children Classes public Data(String line) { //first declare all variables in sub classes if (id == 0) throw new NotSupportedException("You are not allowed to use this constructor for creating the first instance!"); id = num++; SetVariables(); parseLine(line); } And i also have a Sub Class extending this Class. class DienstGruppe : Data {

how to create an instance of class and set properties from a Bag object like session

徘徊边缘 提交于 2020-01-05 09:27:34
问题 the class will be declared in runtime and values are stored in a Bag object like session or ViewBag . Now I want to create an instance of the class and set it's properties using Bag data . I know that I should use reflection , but I don't know if there is any method out of box that does such things or I should create one ? session["Foo"] = "foo"; session["Bar"] = "bar"; var instance = System.Activator.CreateInstance(Type.GetType(className)); instance = ? // how to set properties using session

How do i use Activator.CreateInstance with strings?

て烟熏妆下的殇ゞ 提交于 2019-12-31 19:49:29
问题 In my reflection code i hit a problem with my generic section of code. Specifically when i use a string. var oVal = (object)"Test"; var oType = oVal.GetType(); var sz = Activator.CreateInstance(oType, oVal); Exception An unhandled exception of type 'System.MissingMethodException' occurred in mscorlib.dll Additional information: Constructor on type 'System.String' not found. I tried this for testing purposes and it occurs in this single liner too var sz = Activator.CreateInstance("".GetType(),

What is the expected LifeStyle of a Castle Windsor component activator?

好久不见. 提交于 2019-12-25 06:33:08
问题 I'm using Castle Windsor and DynamicProxy to implement persistence Lazy Loading from scratch (I know NHibernate could be an option etc.) I have implemented a custom component activator to always instantiate my business classes as proxies. I found that the default mixin proxies automatically created when using interceptors were not being used when class methods are called from inside the class itself, which was a problem. So I inherited DefaultComponentActivator and overriding CreateInstance()

Activator.CreateInstance causing nunit to lock which prevents visual studio building

帅比萌擦擦* 提交于 2019-12-24 01:19:00
问题 After I run my test that runs my suspect code; I cannot rebuild the assembly in Visual Studio until Nunit (or more specifically nunit-agent.exe) is ended. The error is: Could not copy "C:\path\MyTests\MyTests.dll" to "bin\Debug\MyTests.dll". Exceeded retry count of 10. Unable to copy file "C:\path\MyTests\Debug\MyTests.dll" to "bin\Debug\MyTests.dll". The process cannot access the file 'bin\Debug\MyTests.dll' because it is being used by another process. Current workaround is to close nunit,