3

I thought I can get an reference to an object contained in the EXE if I load the .net assembly(which is an exe) into my own appdomain and get the object reference through refleciton.

Can I really do this?

Here is my sample code...

myDomain = AppDomain.CreateDomain("MyAppDomain");
myDomain.ExecuteAssembly(exePath);

Assembly myAssembly = myDomain.GetAssemblies().Single(a => a.FullName.Contains("TestAssembly"));

Type t = commandsAssembly.GetType("TestClass");

By any way can I get a reference to an object of this type?

Edit: In the main method of my EXE I am creating an object of type TestClass, I want a reference to that object. What I thought is ExecuteAssembly will execute the exe in the new appdomain, so when the EXE is instantiated my object would be created. Please correct me if I am wrong. CreateInstance will create a new object but I want reference to my object which gets created when EXE is executed... May be I am thinking in a stupid way, please correct me...

Thanks in Advance

coolcake
  • 2,917
  • 8
  • 42
  • 57
  • ExecuteAssembly doesn't come back beforde the end of the Main method... will your TestClass reference be still there or already disposed ? Please explain more clearly why you want to do what you describe. – Yahia Jul 22 '11 at 01:25
  • Yahia@ This is just sample code, i shall call ExecuteAssembly in another thread and shall use the myDomain, to get the references to the real objects running in that domain. I thought if I can create a mini framework to do UI Test automation. To be straightforward, I want to grab all the living instances of a type in an appdomain. – coolcake Jul 25 '11 at 05:15
  • see my edit... if you really need to do this there are options, you would "just" need to do what a good (memory) profiler has to do - at least as a starting point... – Yahia Jul 25 '11 at 09:55

3 Answers3

3

Simple way:

object instance = Activator.CreateInstance(myType, [constructorArg1, ...]);

It assumes a public constructor on the type.

Rex M
  • 142,167
  • 33
  • 283
  • 313
  • In the main method of my EXE I am creating an object of type TestClass, I want a reference to that object. What I thought is ExecuteAssembly will execute the exe in the new appdomain, so when the EXE is instantiated my object would be created. Please correct me if I am wrong. CreateInstance will create a new object but I want reference to my object which gets created when EXE is executed... – coolcake Jul 22 '11 at 01:17
  • @coolcake hm, that's a bit weird. you would need a handle to some root object in the AppDomain and recursively search through the graph to find the reference you want. – Rex M Jul 22 '11 at 01:20
  • Thanks alot Rex, thinking that there is just a simple class, how can I get a reference to it? Can't I use get type and retrieve it? – coolcake Jul 22 '11 at 01:25
  • @coolcake without some pretty sophisticated trickery (way beyond normal reflection) I don't know of any way to simply "grab all the living instances of a type in an appdomain" – Rex M Jul 22 '11 at 01:29
  • Thanks for making me clear. Thats what my question. So I cannot get living instances of a type in an appdomain... I thought I may get it by some way... Thanks a lot... – coolcake Jul 22 '11 at 01:32
2

Try commandsAssembly.CreateInstance ("TestClass"); or Activator.CreateInstance (t);

EDIT - after the addition that all living instances are of interest:

I think the only to achieve this is to write some sort of "debugger" which is really a tough call... some potentially usefull links

http://blogs.msdn.com/b/davbr/archive/2011/02/01/clrprofiler-v4-released.aspx
http://www.codeproject.com/Articles/122592/Writing-a-NET-debugger-part-1-Starting-the-debuggi
http://www.codeproject.com/Articles/122591/Writing-a-NET-debugger-part-2-Handling-events-and
http://www.codeproject.com/Articles/126142/Writing-a-net-debugger-part-3-symbol-and-source-fi
http://www.codeproject.com/Articles/132798/Writing-a-NET-debugger-part-4-breakpoints
http://msdn.microsoft.com/en-us/library/bb190764.aspx
http://msdn.microsoft.com/en-us/library/ms229861.aspx

The CLR profiler needs to do parts of you want - source is available so perhaps a good start...

valiano
  • 16,433
  • 7
  • 64
  • 79
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • Thank you. I was looking for this kind of stuff. I shall go through your links and work on my stuff. – coolcake Jul 25 '11 at 23:15
1

Yes, try this:

object obj = Activator.CreateInstance(Type.GetType("[AssemblyName].LoadClass, [AssemblyName]", true));

Also look at this for a good discussion: C# - Correct Way to Load Assembly, Find Class and Call Run() Method

Community
  • 1
  • 1
Mrchief
  • 75,126
  • 20
  • 142
  • 189