0

I am trying to use Reflection to create my objects at run time. However I keep getting

System.TypeLoadExeption

Could not load type 'x' from assembly 'y', version = 1.0.0.0, culture = neutral PublicKeyToken=null'. I just create a new project to try it and it still wont work even though i have not changed the namespace names.

ObjectHandle keyword = Activator.CreateInstance("NamespaceProject", "ChangeDue");
Object myObject = keyword.Unwrap();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
a-exelle
  • 11
  • 5
  • 1
    You should provide `AssemblyName` instead of `NamespaceProject` https://learn.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=netframework-4.8#System_Activator_CreateInstance_System_String_System_String_ – Dmitry Bychenko Mar 05 '20 at 13:26
  • See the accepted answer in the duplicate, in particular the second example. – MakePeaceGreatAgain Mar 05 '20 at 13:31
  • @DmitryBychenko that is the assembly name – a-exelle Mar 05 '20 at 13:41
  • Look if `typeof(ChangeDue).AssemblyQualifiedName` returns the assembly name you are expecting. Note, `ChangeDue` looks more like a property name to me. You must provide a type name (class name or struct name). – Olivier Jacot-Descombes Mar 05 '20 at 13:54
  • @OlivierJacot-Descombes FFS it is a class. i am trying to make the name obscure because it is code for my job – a-exelle Mar 05 '20 at 13:55
  • @OlivierJacot-Descombes when i check the qualified name it says NamespaceProject.ChangeDue, NamespaceProject, version=1.0.0.0, culture = neutral, publickeytoken = null. Also if i change the assembly name to Namespace.ChangeDueDate i get file not found exception – a-exelle Mar 05 '20 at 14:00
  • I figured it out im supposed to do "AssemblyName.Type" for the second parameter. – a-exelle Mar 05 '20 at 14:04
  • Try `Activator.CreateInstance("NamespaceProject", "NamespaceProject.ChangeDue")`, i.e., qualify the name of the type with the namespace. It's `"Namespace.Type"` for the second parameter. – Olivier Jacot-Descombes Mar 05 '20 at 14:05
  • @OlivierJacot-Descombes when i did it that way i dont get an exception but i have an object of type Namespace.ChangeDue instead of ChangeDue – a-exelle Mar 05 '20 at 14:28
  • Which namespace should `ChangeDue` have then? Do you have more than one `ChangeDue` type? If not, then `ChangeDue` is the same type as `NamespaceProject.ChangeDue`. Only if you have more than one such type in different namespaces, this could make a difference. The namespace does not make a type any different. It is only there to differentiate different types having the same name. I.e., There is a type `System.DateTime` and you could create another one `MyNamepsace.DateTime`. – Olivier Jacot-Descombes Mar 05 '20 at 16:09

0 Answers0