| | |
Retreiving/Allocating a new type during runtime with Reflection
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 103
Reputation:
Solved Threads: 17
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:
Hope this is enough information to get my point across.
Thanks
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:
C# Syntax (Toggle Plain Text)
public void RunClass(string className) { Assembly assemb = Assembly.SomewayToGetCurrentAssembly; SomeObject = assemb.CreateInstance(ofTypeNeededBasedOnSTring) as Something?; //... MethodInfo meth = SomeObject.GetMethod("ExecuteTask"); }
Hope this is enough information to get my point across.
Thanks
•
•
Join Date: Apr 2007
Posts: 103
Reputation:
Solved Threads: 17
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.
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:
Just some placeholder stuff to get the method invoke working.
Thanks,
C# Syntax (Toggle Plain Text)
public void RunScript(string scriptName) { Assembly _assemblyInfo = Assembly.LoadFrom(scriptName); Type[] types = _assemblyInfo.GetTypes(); Type temp = null; foreach (Type t in types) { if (t.FullName == "Scripting.Test.Autosniff") { Console.Write("this may actually work\n"); temp = t; break; } } MethodInfo meth; meth = temp.GetMethod("RunScript"); meth.Invoke(???, null); }
BTW, the RunScrpt Method is:
C# Syntax (Toggle Plain Text)
public override void RunScript() { x = 5; y = 10; z = 15; }
Thanks,
•
•
Join Date: Apr 2007
Posts: 103
Reputation:
Solved Threads: 17
Figured it out
C# Syntax (Toggle Plain Text)
public void RunScript(string scriptName) { Assembly _assemblyInfo = Assembly.LoadFrom("Script.dll"); Type theType = _assemblyInfo.GetType(scriptName); MethodInfo meth; meth = theType.GetMethod("ExecuteScript"); object obj = Activator.CreateInstance(theType); meth.Invoke(obj, null); }
Last edited by mariocatch; Jun 22nd, 2007 at 1:58 am.
![]() |
Similar Threads
- COMException error, class not registered (VB.NET)
- Creating ENUM Runtime (Visual Basic 4 / 5 / 6)
- hangman revised (VB.NET)
- i have tried, but am getting no where, i've spent hours on it, please help (C++)
- Big Problem, Generic Error (VB.NET)
Other Threads in the C# Forum
- Previous Thread: How do I pinvoke this function?
- Next Thread: Help me coding a ChekBox
| Thread Tools | Search this Thread |
.net access algorithm array backup barchart bitmap box broadcast c# check checkbox client clock combobox control conversion csharp custom database databasesearch datagrid datagridview datagridviewcheckbox dataset datetime degrees development draganddrop drawing dynamiccreation encryption enum equation excel file form format formatting forms function gdi+ hospitalmanagementsystems httpwebrequest image index input install interface java label list listbox mandelbrot math microsystems mouseclick mysql namevaluepairs operator path photoshop picturebox pixelinversion post powerpacks programming property radians regex remoting resource restore richtextbox server sleep soap socket sql sqlserver statistics stream string table text textbox thread time timer update usercontrol validation visualstudio wait webbrowser windows winforms working wpf xml





