Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi All,

I am going to create a method which will require any custom class reference (not object)to Execute.
class A
{
}
class B
{
}

class C
{
  pubilc void RunTimeMethod(Parameter) // Here I want to accept Class A or Class B as parameter at runtime
  {
// Method is working fine if enter I use RuntimeMethod(A obja). But It can be any one class
  }
}
class D:C
{
//C objC = new C();
RunTimeMethod(class);  // Here I want to pass Class A or Class B as parameter at runtime
//RuntimeMethod(A) working fine.
}

So, How to define pass class to method while creating and calling it?

Regards!
Aman
Posted
Updated 11-Aug-11 2:22am
v3
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 3:26am    
What is "class reference (not object)"? There is not such thing. There is System.Type (you can get by typeof). What is "class reference to method"?
This is not just terminology -- you might not understand what are you asking about. Try to use "Improve question" above, if you can.

--SA
Aman4.net 3-Aug-11 3:39am    
Question is updated

Try:
C#
public static void RunSnippet()
   {
   Button b = new Button();
   Console.WriteLine(myMethod<Button>(b));
   }
public static string myMethod<T>(T t) where T : Control
   {
   return t.ToString();
   }
 
Share this answer
 
Comments
BobJanova 3-Aug-11 12:25pm    
Just as a note, you can do
Console.WriteLine(myMethod(b));

The type parameter T will be inferred from the argument.
OriginalGriff 3-Aug-11 14:15pm    
Doh! It's been a long day...
This is not very useful idea simply because the classes has very little in common: only System.Object as a common base class. The answer depends on what you can do in RunTimeMethod. Not many. You can only call methods of System.Object.

It would look like this:
C#
internal void RunTimeMethod<T>(T t) where T: new()
//the "new" above will guarantee a class with parameter-less constructor
{
   //...
}


As a more advanced approach, you can use Reflection and find out some general purpose constructor. But it's much more robust to demand some common interface for any of the parameter classes and put it in a generic constraint.

C#
internal interface IGenericParameterClass { /*...*/ }
internal class A : IGenericParameterClass { /*...*/ }
internal class B : IGenericParameterClass { /*...*/ }

internal void RunTimeMethod<T>(T t) where T: IGenericParameterClass, new()
//the IGenericParameterClass above will a generic parameter with common interface to use
//in this function
{
   //
}



Also, think about using classic OOP or interface-based approach. You might be trying to heavily abuse OOP in your approach. Just think about it.

Also, if you can explain the ultimate goal of all this activity, you can get a chance to get better advice.

—SA
 
Share this answer
 
v3
Comments
Wonde Tadesse 3-Aug-11 22:15pm    
5+
Sergey Alexandrovich Kryukov 3-Aug-11 22:37pm    
Thank you, Wonde.
--SA
Asish Limbu 4-Aug-11 0:00am    
Good Stuff.+5.
Sergey Alexandrovich Kryukov 4-Aug-11 0:17am    
Thank you, Asish.
--SA
Aman4.net 11-Aug-11 8:20am    
Thanx for such good answer, Bascially Class is being stored in Xml after serialization for further use with different technologies. 5+
Here is an answer with above example

class A
{
}
class B
{
}

class C
{
pubilc void RunTimeMethod<T>() where T: class
{
}
}
class D:C
{
B objB = new B();
if(objB==null)
{
RunTimeMethod<A>();
}
else
{
RunTimeMethod<B>();
}
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900