-2

I have an object a created by someone else using the following

 classA a = new classA()

However I don't know a is classA or not inside some_function.

void some_function(Object a)
{

}

I am wondering from a, how can I get classA, and then call its static method?

Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • Well you can call `a.getClass()` to get the class, then reflection to call the method - but if you already know it, why can't you just use `classA.staticMethod()`? You haven't really provided enough context here... – Jon Skeet Mar 22 '15 at 09:36
  • a can be created by someone else; I only have a passed in. – Adam Lee Mar 22 '15 at 09:39
  • Right, so add all of that as context to the question. The reason your two answers so far haven't helped you is that the question isn't clear enough. – Jon Skeet Mar 22 '15 at 09:42
  • modified; hopeful it is clearer. – Adam Lee Mar 22 '15 at 09:43
  • Not much. It would be more helpful if you would show the declaration of the method you're trying to write, where presumably you have a parameter of type `Object`. Ideally, show a short but complete program demonstrating the problem. How do you know which method to call? Note that your title is very misleading, talking about instance variables... I see no indication that instance variables are useful here. – Jon Skeet Mar 22 '15 at 09:45
  • modified again, actually you are right; I am inside some function accept a as Object. – Adam Lee Mar 22 '15 at 09:55
  • also Jon, no getClass() method; only getType() method. – Adam Lee Mar 22 '15 at 10:03
  • Whoops, yes - that's one problem with not following naming conventions in your question - I thought it was Java not C#, having missed the tag... – Jon Skeet Mar 22 '15 at 10:29
  • And I still don't see a short but complete program or any way of knowing what the method you want to call looks like. Please read http://tinyurl.com/stack-hints – Jon Skeet Mar 22 '15 at 10:30

1 Answers1

0

For using static methods, you do not need to instantiate the class. Just call it using class name as variable.

If you are passed variable a then get the class same using

t = a.GetType().Name ;

if(t=="classA"){
  classA.thefunction();
} else if(t="classB){
  classB.thefunction();
}  //and so not...

and instantiate the class.

Ashutosh Nigam
  • 868
  • 8
  • 27
  • I know it; but I only have instance a; also, I am not sure it is classA. I would like to determine its class first. – Adam Lee Mar 22 '15 at 09:40
  • thanks and it is close to my need; but the solution looks a little bit ugly, do you think so? I have to use a lot of if .. else? Is there better way? – Adam Lee Mar 22 '15 at 10:04
  • reflection can be used to get the class reference cleanly, MethodInfo will help in getting the static method and invoke to run it. there is a stackoverflow link for it: http://stackoverflow.com/questions/1044455/c-sharp-reflection-how-to-get-class-reference-from-string – Ashutosh Nigam Mar 22 '15 at 10:38
  • @AdamLee glad to know my answer helped you. I also proposed you advanced way to use in your current situation. Do not forget to accept my answer. – Ashutosh Nigam Mar 23 '15 at 07:05