Reputation: 5397
I have a static method whose internal code is capable of working with multiple different classes. However I am unsure how to get the classes into the method at run time.
For example lets say I have ClassA and ClassB
My method can work with either of these classes and so I want the parameters to look something like.
public static void MyMethod(Object obj)
{
//...obj is either ClassA or ClassB
//...do work on variable class
}
But I keep getting an invalid argument message when I try either
ClassA a = new ClassA();
MyMethod(a);
ClassB b = new ClassB();
MyMethod(b)
Is it even possible to have a parameter who's Type is unknown until runtime?
If so how do I do this?
If not, how can a method that performs some function on many classes by utilised in this way, without repeating the method code in every class?
EDIT - I have edited the above as my code DOES indeed pass in an instance of a class, and not just the class name. However I still do get the Invalid argument message.
Upvotes: 0
Views: 227
Reputation: 5397
Actually the error was here -
public static void MyMethod(Object obj)
Once I changed the parameter as follows:
public static void MyMethod(object obj)
it worked fine :)
Upvotes: 0
Reputation: 7080
If ClassA and ClassB have same function signatures, consider using Interfaces. If implementations are same as well, implement them in base abstract class and override them in inherited classes if required.
interface IMyInterface
{
void MethodX();
}
abstract class ClassBase: IMyInterface
{
public virtual void MethodX()
{
Console.WriteLine("MethodX from base class");
}
}
// this will use base class implementation of MethodX
class ClassA : ClassBase
{
}
class ClassB : Classbase
{
public void MethodX()
{
Console.WriteLine("MethodX from ClassB");
}
}
class Foo
{
public void MyMethod(IMyInterface obj)
{
obj.MethodX();
}
}
Foo f = new Foo();
IMyInterface ca = new ClassA();
f.MyMethod(ca); // This Print MethodX from ClassA, in this case BaseClass
IMyInterface cb = new ClassB();
f.MyMethod(cb); // This Print MethodX from ClassB
Upvotes: 0
Reputation: 508
You should overload MyMethod
as suggested by Richard.
If you still want to use just one method definition, you could use something like:
public static void UnknownArgumentsMethod2(params object[] list)
{
foreach (object o in list)
{
if (o.GetType() == typeof(int))
{
Console.WriteLine("This is an integer: " + (int)o);
}
else if (o.GetType() == typeof(ClassA))
{
Console.WriteLine("This is an object of ClassA");
}
}
}
Upvotes: 0
Reputation: 35477
You need to pass an instance (object) of the class. Try:
MyMethod(new ClassA());
MyMethod(new ClassB());
I would suggest overloading
MyMethod, such as:
public void MyMethod(ClassA a) { ... }
punlic void MyMethod(ClassB b) { ... }
Upvotes: 1
Reputation: 10680
You are passing a type, not an instance of a type.
Consider this instead:-
var test = new ClassA();
MyMethod(test);
Upvotes: 0
Reputation: 1062494
Assuming ClassA
is a type name, then that syntax is not valid; you must either pass in an instance of the type, or the Type
of the type:
MyMethod(new ClassA());
or
MyMethod(typeof(ClassA));
For the latter case, you could also consider refactoring to use generics:
MyMethod<ClassA>();
Is it even possible to have a parameter who's Type is unknown until runtime?
Absolutely; object
would be fine, but you need to give that parameter a valid value.
Upvotes: 2