Reflection

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

Join Date: Apr 2007
Posts: 24
Reputation: obscured47 is an unknown quantity at this point 
Solved Threads: 0
obscured47 obscured47 is offline Offline
Newbie Poster

Reflection

 
0
  #1
Nov 12th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Reflection

 
0
  #2
Nov 12th, 2008
Pass in to the function in class A a delegate or object that constructs class B.

  1. abstract class A {
  2. public static void INeedToConstructSomeSubclass(Func<A> subclassDelegate, ...) {
  3. A blah = subclassDelegate();
  4. ...
  5. }
  6. }

Then just call it with
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 172
Reputation: Jugortha is an unknown quantity at this point 
Solved Threads: 16
Jugortha Jugortha is offline Offline
Junior Poster

Re: Reflection

 
0
  #3
Nov 13th, 2008
Or simply you can use interfaces to play the role of bridge
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Reflection

 
1
  #4
Nov 13th, 2008
You can walk through a library looking for classes that descend from something, and then instantiate those.

  1. CommandTemplate newcmd;
  2. Console.Write("Loading commands");
  3. Assembly a = null;
  4. AssemblyName n = new AssemblyName("mycmds");
  5. a = Assembly.Load(n);
  6. foreach (Type t in a.GetTypes())
  7. {
  8.  
  9. if (t.BaseType.Name == "CommandTemplate")
  10. {
  11. newcmd = (CommandTemplate)Activator.CreateInstance(t);
  12.  
  13. }
  14. }
  15. 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 24
Reputation: obscured47 is an unknown quantity at this point 
Solved Threads: 0
obscured47 obscured47 is offline Offline
Newbie Poster

Re: Reflection

 
0
  #5
Nov 15th, 2008
Sorry for not replying earlier. What LizR described is what I was pretty much lookingfor. Cheers
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