activator

C# Using Activator.CreateInstance

江枫思渺然 提交于 2019-11-28 15:06:55
I asked a question yesterday regarding using either reflection or Strategy Pattern for dynamically calling methods. However, since then I have decided to change the methods into individual classes that implement a common interface. The reason being, each class, whilst bearing some similarities also perform certain methods unique to that class. I had been using a strategy as such: switch (method) { case "Pivot": return new Pivot(originalData); case "GroupBy": return new GroupBy(originalData); case "Standard deviation": return new StandardDeviation(originalData); case "% phospho PRAS Protein":

Given “where T : new()”, does “new T()” use Activator.CreateInstance internally?

会有一股神秘感。 提交于 2019-11-28 10:53:57
If I have a type parameter constraint new() : void Foo<T>() where T : new() { var t = new T(); } Is it true that new T() will internally use the Activator.CreateInstance method (i.e. reflection)? Yes, this is true. Edit 2: Here's a good explanation of the how and why. http://www.simple-talk.com/community/blogs/simonc/archive/2010/11/17/95700.aspx For verification I compiled the following method: public static T Create<T>() where T: new() { return new T(); } And this is the generated IL when compiled with the C# compiler in .NET 3.5 SP1: .method public hidebysig static !!T Create<.ctor T>() cil

Does System.Activator.CreateInstance(T) have performance issues big enough to discourage us from using it casually?

假装没事ソ 提交于 2019-11-27 19:40:36
Does System.Activator.CreateInstance(T) method have performance issues (since I'm suspecting it uses reflection) big enough to discourage us from using it casually? As always, the only correct way to answer a question about performance is to actually measure the code. Here's a sample LINQPad program that tests: Activator.CreateInstance new T() calling a delegate that calls new T() As always, take the performance program with a grain of salt, there might be bugs here that skews the results. The output (timing values are in milliseconds): Test1 - Activator.CreateInstance<T>() 12342 Test2 - new T

How to use Activator to create an instance of a generic Type and casting it back to that type?

人盡茶涼 提交于 2019-11-27 07:47:16
I have a generic type Store<T> and use Activator to make an instance of this type. Now how, after using the Activator, can I cast the resulted object of type object back to the instantiated type? I know the type that I used to instantiate the generic. Please see the following code: class Store<T> where T : IStorable {} class Beer : IStorable {} class BeerStore : Store<Beer> {} Type storeType = someObjectThatImplementsIStorable.GetType(); Type classType = typeof(Store<>); Type[] typeParams = new Type[] { storeType }; Type constructedType = classType.MakeGenericType(typeParams); object x =

How to access Microsoft Word existing instance using late binding

瘦欲@ 提交于 2019-11-27 04:55:42
i am developing some code in c# where i will be interacting with Microsoft Word. I want to be able to have the option of re-using an existing instance or as an alternative creating a new instance. Keeping in mind i want to do all of this using LATE BINDING... it is safe to say i have figured out how to get things working when creating a new instance.. i just call Activator.CreateInstance etc.. The problem i am having is how do i reuse an existing instance, for example, Word is already open and i want to use that instance. Is there an Activator.UseExistingInstance? or something similar?? Thanks

.NET: Unable to cast object to interface it implements

依然范特西╮ 提交于 2019-11-27 04:32:37
问题 I have a class (TabControlH60) that both inherits from a base class (UserControl) and implements an interface (IFrameworkClient). I instantiate the object using the .NET Activator class. With the returned instance, I can cast to the UserControl base class, but not to the interface. The exception I get is below the code snipet. How do I cast to the interface? object obj = Activator.CreateInstance(objType); Type[] interfaces = obj.GetType().GetInterfaces(); // contains IFrameworkClient m_Client

Given “where T : new()”, does “new T()” use Activator.CreateInstance internally?

拥有回忆 提交于 2019-11-27 03:54:02
问题 If I have a type parameter constraint new() : void Foo<T>() where T : new() { var t = new T(); } Is it true that new T() will internally use the Activator.CreateInstance method (i.e. reflection)? 回答1: Yes, this is true. Edit 2: Here's a good explanation of the how and why. http://www.simple-talk.com/community/blogs/simonc/archive/2010/11/17/95700.aspx For verification I compiled the following method: public static T Create<T>() where T: new() { return new T(); } And this is the generated IL

Does System.Activator.CreateInstance(T) have performance issues big enough to discourage us from using it casually?

旧巷老猫 提交于 2019-11-26 19:57:52
问题 Does System.Activator.CreateInstance(T) method have performance issues (since I'm suspecting it uses reflection) big enough to discourage us from using it casually? 回答1: As always, the only correct way to answer a question about performance is to actually measure the code. Here's a sample LINQPad program that tests: Activator.CreateInstance new T() calling a delegate that calls new T() As always, take the performance program with a grain of salt, there might be bugs here that skews the

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 use Activator to create an instance of a generic Type and casting it back to that type?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 13:48:42
问题 I have a generic type Store<T> and use Activator to make an instance of this type. Now how, after using the Activator, can I cast the resulted object of type object back to the instantiated type? I know the type that I used to instantiate the generic. Please see the following code: class Store<T> where T : IStorable {} class Beer : IStorable {} class BeerStore : Store<Beer> {} Type storeType = someObjectThatImplementsIStorable.GetType(); Type classType = typeof(Store<>); Type[] typeParams =