Questions tagged [activator]

Activator is a .NET class that can create dynamically-typed object instances at runtime.

278 questions
154
votes
5 answers

How to dynamically create generic C# object using reflection?

In C# I have the following object: public class Item { } public class Task { } public class TaskA : Task { } public class TaskB : Task { } I want to dynamically create TaskA or TaskB using C# reflection (Activator.CreateInstance).…
Jeff
  • 13,079
  • 23
  • 71
  • 102
55
votes
6 answers

C# Using Activator.CreateInstance

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…
Darren Young
  • 10,972
  • 36
  • 91
  • 150
50
votes
5 answers

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

I have a generic type Store 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…
Bazzz
  • 26,427
  • 12
  • 52
  • 69
39
votes
6 answers

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

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…
38
votes
5 answers

How do i use Activator.CreateInstance with strings?

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…
user34537
36
votes
5 answers

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

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?
Pacerier
  • 86,231
  • 106
  • 366
  • 634
32
votes
9 answers

.NET: Unable to cast object to interface it implements

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…
user193327
  • 341
  • 1
  • 3
  • 4
27
votes
10 answers

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

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…
TimothyP
  • 21,178
  • 26
  • 94
  • 142
20
votes
6 answers

Can I use Activator.CreateInstance with an Interface?

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…
Arman Hayots
  • 2,459
  • 6
  • 29
  • 53
14
votes
2 answers

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

If I have a type parameter constraint new(): void Foo() where T : new() { var t = new T(); } Is it true that new T() will internally use the Activator.CreateInstance method (i.e. reflection)?
msfanboy
  • 5,273
  • 13
  • 69
  • 120
14
votes
5 answers

Activator and static classes

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…
japollock
  • 1,010
  • 1
  • 8
  • 14
14
votes
6 answers

Set property Nullable<> by reflection

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…
Cédric Boivin
  • 10,854
  • 13
  • 57
  • 98
10
votes
3 answers

Why is Activator.CreateInstance() allowed without the new() generic type constraint?

In the sample code shown below, the "CompileError" method won't compile, because it requires the where T : new() constraint as shown in the CreateWithNew() method. However, the CreateWithActivator() method compiles just fine without a…
Hydrargyrum
  • 3,378
  • 4
  • 27
  • 41
9
votes
2 answers

How can I use Activator.CreateInstance to create a List where T is unknown at runtime?

I'm using Activator.CreateInstance to create objects by a type variable (unknown during run time): static dynamic CreateFoo( Type t ) => Activator.CreateInstance( t ); Obviously, I do not yet properly understand how to use the dynamic type,…
Will
  • 3,413
  • 7
  • 50
  • 107
9
votes
1 answer

Activator.CreateInstance creates value of type T instead of Nullable

Look at the sample code below var genericNullableType = typeof(Nullable<>); var nullableType = genericNullableType.MakeGenericType(typeof(bool)); var returnValue = Activator.CreateInstance(nullableType, (object)false); For some reason returnValue…
Demarsch
  • 1,419
  • 19
  • 39
1
2 3
18 19