7

I've seen this Topic : Creating an instance from a class name

and written this code:

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { object obj = Activator.CreateInstance(null, "MyClass"); MyClass t = (MyClass)obj; t.My1 = 100; MessageBox.Show(t.My1.ToString()); } } public class MyClass { public int My1 { get; set; } public int My2 { get; set; } } 

However when its runs there's an exception:

Could not load type 'MyClass' from assembly 'Test_Reflection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. 

I have another question. I have a class in one assembly that has some property. In another assembly I want create instance of it and get access to it's properties, by typing one of them just using stringy Class Name. How can I do that?

7
  • 1
    Are you sure you actually read the answer you are linking to? Commented Mar 12, 2011 at 13:02
  • 2
    Try to specify the namespace. Commented Mar 12, 2011 at 13:04
  • Yes.I pass in null because MyClass is locate in Current Assembly Commented Mar 12, 2011 at 13:05
  • @AS-CII: Why null cause exception? Commented Mar 12, 2011 at 13:05
  • For your second question, you just need to search SO on reflection. Commented Mar 12, 2011 at 13:43

2 Answers 2

15

According to MSDN null actually doesn't mean current assembly. It means that assembly will be searched (its matter when your class is located in another assembly). Also you need specify not only the class name. So, to prevent searching and get type correctly you need to write full assembly-qualified name:

Type objType = Type.GetType("YourNamespace.MyClass, YourAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); object obj = Activator.CreateInstance(objType); MyClass t = (MyClass)obj; 

Assembly-qualified name you can retrieve for example with next code (to check that you are not mistaken):

string name = typeof(MyClass).AssemblyQualifiedName; 
Sign up to request clarification or add additional context in comments.

5 Comments

Actually it does (as long as current == executing?). If assemblyName is null, the executing assembly is searched.
The first two sentences don't make sense to me. Can you clarify? If it doesn't mean current assembly, what does it mean?
@Ritch Melton, when MyClass located in the current assembly - no matter how to write. But when you need to use class from another assembly - null will cause searching for that assembly. Much better to specify it explicitly.
@archer: Thanks alot.please guide me for second question.
@Nima, if i properly understand your question, then you need to add reference to that assembly, add appropriate using statement of namespace with that class and write similar code as in example in my answer. Just set in assembly-qualified name string correct values.
2

You just need to prepend the namespace to the class name. In a console exe project, this works for me. You did have a problem with the way you were using the returned object handle. It's not an Object, but an ObjectHandle and you need to call Unwrap() get at the actual type instance.

namespace CSharpConsoleTest { public class MyClass { public int My1 { get; set; } public int My2 { get; set; } } public class Program { public static void Main(string[] args) { var obj = Activator.CreateInstance(null, "CSharpConsoleTest.MyClass"); var t = (MyClass)obj.Unwrap(); t.My1 = 100; MessageBox.Show(t.My1.ToString()); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.