| | |
Reflection
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 24
Reputation:
Solved Threads: 0
Hi guys,
I have a type that implements an abstract class that is known at compile time and I want to construct at run time an instance of the type and place the reference in a variable that is of the abstract class's type.
So, to describe it a bit.. Say we have 2 class libraries (called A and B). Library A holds the abstract class and library B holds an implementation of the abstract class. A static variable in the abstract class needs to construct at run time a B object and save it in an A type variable... that is, an A type doesn't know about the existance of B on compile time..
So.. I want to read up on app domains and reflection and all those joys of the .NET but don't have the necessary time right now to do it properly so need you guys to guide me a bit on this. Hope it makes (some) sense.
Cheers!
I have a type that implements an abstract class that is known at compile time and I want to construct at run time an instance of the type and place the reference in a variable that is of the abstract class's type.
So, to describe it a bit.. Say we have 2 class libraries (called A and B). Library A holds the abstract class and library B holds an implementation of the abstract class. A static variable in the abstract class needs to construct at run time a B object and save it in an A type variable... that is, an A type doesn't know about the existance of B on compile time..
So.. I want to read up on app domains and reflection and all those joys of the .NET but don't have the necessary time right now to do it properly so need you guys to guide me a bit on this. Hope it makes (some) sense.
Cheers!
Pass in to the function in class A a delegate or object that constructs class B.
Then just call it with
You'll need some way of communicating which subclass needs to be constructed -- and it's better and more typesafe to pass in a function than something that uses reflection.
C# Syntax (Toggle Plain Text)
abstract class A { public static void INeedToConstructSomeSubclass(Func<A> subclassDelegate, ...) { A blah = subclassDelegate(); ... } }
Then just call it with
C# Syntax (Toggle Plain Text)
A.INeedToConstructSomeSubclass(() => new B());
You'll need some way of communicating which subclass needs to be constructed -- and it's better and more typesafe to pass in a function than something that uses reflection.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
You can walk through a library looking for classes that descend from something, and then instantiate those.
C# Syntax (Toggle Plain Text)
CommandTemplate newcmd; Console.Write("Loading commands"); Assembly a = null; AssemblyName n = new AssemblyName("mycmds"); a = Assembly.Load(n); foreach (Type t in a.GetTypes()) { if (t.BaseType.Name == "CommandTemplate") { newcmd = (CommandTemplate)Activator.CreateInstance(t); } } Console.WriteLine(" Done");
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
![]() |
Similar Threads
- Reflection Invoke Method w/ Params (C#)
- problem with reflection (Java)
- Error:Reflection.targetException (ASP.NET)
- phpunit installation says about reflection extension (PHP)
- Retreiving/Allocating a new type during runtime with Reflection (C#)
- Reflection in Python (Java)
- System.Reflection (C#)
- hijackthis log (Viruses, Spyware and other Nasties)
- Programming Professor (IT Professionals' Lounge)
Other Threads in the C# Forum
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development dll draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser wia windows winforms wpf xml






