Questions tagged [createinstance]

126 questions
291
votes
5 answers

How to Pass Parameters to Activator.CreateInstance()

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() doesn't see to…
DaveDev
  • 41,155
  • 72
  • 223
  • 385
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…
12
votes
8 answers

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

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…
thames
  • 5,833
  • 6
  • 38
  • 45
11
votes
2 answers

ruby on rails, creating new object, use create or new method?

I am trying to create an object via an API, i.e. no forms are required, should I be doing MyModel.new(:name => params[:name]) or MyModel.create(:name => params[:name]) ? Assume I have resources : my_models in routes I checked and I see that methods…
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
8
votes
2 answers

Dynamically create an array and set the elements

This can create an array dynamically: Assembly asm = object.GetType().Assembly; string sTypeName = "Company.Namespace.ClassName"; object arrayWithSize1 = Activator.CreateInstance( asm.GetType(sTypeName), 1 ); But how does set the first element of…
uzay95
  • 16,052
  • 31
  • 116
  • 182
7
votes
1 answer

The specified record cannot be mapped to a managed value class

Hi have a vb6 project "PROJVB6" with a class "CLASSVB6" with this content: ... Public Type ActualParametersType_DOCUMENTI Codice As Variant Numreg As Variant End Type ... Private Gvar_ActualParameters As…
Luca Romagnoli
  • 12,145
  • 30
  • 95
  • 157
7
votes
1 answer

GetMethod when argument comes by reference

I'm creating an instance of an object using reflection and getting the methods within the class of the object, but then the problem comes when I have to use an array of type Type to avoid ambiguity problems, here is an example of the code that I'm…
Chema Sarmiento
  • 279
  • 2
  • 3
  • 11
6
votes
2 answers

Is it possible to dynamically create form without having *.dfm and *.pas files?

is it possible to create and show TForm without having source files for it ? I want to create my forms at runtime and having the empty *.dfm and *.pas files seems to me useless. Thank you
Martin Reiner
  • 2,167
  • 2
  • 20
  • 34
6
votes
2 answers

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

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…
Wolf5
  • 16,600
  • 12
  • 59
  • 58
5
votes
3 answers

Why does my call to Activator.CreateInstance intermittently fail?

I'm using the following code to access the Windows Explorer Shell's band site service: Guid GUID_TrayBandSiteService = new Guid(0xF60AD0A0, 0xE5E1, 0x45cb, 0xB5, 0x1A, 0xE1, 0x5B, 0x9F, 0x8B, 0x29, 0x34); Type shellTrayBandSiteService =…
Daniel Stutzbach
  • 74,198
  • 17
  • 88
  • 77
5
votes
5 answers

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

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…
e-holder
  • 1,504
  • 4
  • 20
  • 33
4
votes
1 answer

Pyomo Cannot iterate over abstract Set before it has been constructed

I constructed an abstract model in Pyomo and it worked well. However, when I try to use a dictionary to instantiate the abstract model, I got the following errors "ERROR: Rule failed when generating expression for objective value: RuntimeError:…
Wilson
  • 125
  • 8
4
votes
1 answer

Casting to correct type after using Activator.CreateInstance

I am having issues casting to correct type after using Activator.CreateInstance(...) which just returns an object type. So the code I have is as follows. I have an empty interface to define an object as a command.. /// /// Defines the…
Dib
  • 2,001
  • 2
  • 29
  • 45
4
votes
2 answers

Activator.CreateInstance vs Factory Pattern

What is the difference between Activator.CreateInstance and factory? Can they be used interchangeably? Or stil do we need a factory pattern?
Selçuk Öztürk
  • 975
  • 1
  • 14
  • 23
3
votes
1 answer

System.Activator.CreateInstance returning null

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!!!"); …
Diddykonga
  • 31
  • 3
1
2 3
8 9