Retreiving/Allocating a new type during runtime with Reflection

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Retreiving/Allocating a new type during runtime with Reflection

 
0
  #1
Jun 21st, 2007
I've been looking around at various examples of Reflections and couldn't find any that go into detail about the scenario i need.

Let me explain what my current situation is:

Main Console App (passes down a string to Class A)
-> Class A which sole purpose is to take the string from Main Console App, and use it to convert to a specific type.
-> Class B/C/D/E/F/G/etc... These classes all serve one purpose as well, which is to run an executed plan. Class A needs to determine, based on a string from the Main Console App, which instance of these classes to make an instance of, and act upon it. Reflection seems like what I need, i just couldnt find a way to create an insatnce of type 'x', based on what string is passed down from main console app to class A.
-> Class Base -> classes B-Z derive from class Base. Shouldn't really affect the actual question here though, which is about Reflection and the syntax needed to convert to a specific type, based on a string.


from what i've read so far, im going to need to use:

  1. public void RunClass(string className)
  2. {
  3. Assembly assemb = Assembly.SomewayToGetCurrentAssembly;
  4.  
  5. SomeObject = assemb.CreateInstance(ofTypeNeededBasedOnSTring) as Something?;
  6.  
  7. //... MethodInfo meth = SomeObject.GetMethod("ExecuteTask");
  8. }


Hope this is enough information to get my point across.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Retreiving/Allocating a new type during runtime with Reflection

 
0
  #2
Jun 21st, 2007
So I got most of it working - Here's what i'm doing now. I just need some direction/assistance on one line, which is the MethodInfo object.Invoke method.
  1. public void RunScript(string scriptName)
  2. {
  3. Assembly _assemblyInfo = Assembly.LoadFrom(scriptName);
  4. Type[] types = _assemblyInfo.GetTypes();
  5. Type temp = null;
  6. foreach (Type t in types)
  7. {
  8. if (t.FullName == "Scripting.Test.Autosniff")
  9. {
  10. Console.Write("this may actually work\n");
  11. temp = t;
  12. break;
  13. }
  14. }
  15. MethodInfo meth;
  16. meth = temp.GetMethod("RunScript");
  17. meth.Invoke(???, null);
  18.  
  19. }
I've tried passing temp into the first parameter of Invoke, but it fails saying it's of a different type than needed.
BTW, the RunScrpt Method is:
  1. public override void RunScript()
  2. {
  3. x = 5;
  4. y = 10;
  5. z = 15;
  6. }
Just some placeholder stuff to get the method invoke working.
Thanks,
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Retreiving/Allocating a new type during runtime with Reflection

 
0
  #3
Jun 22nd, 2007
Figured it out

  1. public void RunScript(string scriptName)
  2. {
  3. Assembly _assemblyInfo = Assembly.LoadFrom("Script.dll");
  4. Type theType = _assemblyInfo.GetType(scriptName);
  5.  
  6. MethodInfo meth;
  7. meth = theType.GetMethod("ExecuteScript");
  8. object obj = Activator.CreateInstance(theType);
  9. meth.Invoke(obj, null);
  10. }
Last edited by mariocatch; Jun 22nd, 2007 at 1:58 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC