1

Assume there's a base class

class base
{
  int x, y;
}

And 3 derived singleton classes A, B, C with x, y initialized to some value.

example :

class A : base { x = 1; y = 0;}
class B : base { x = 0; y = 1;}    
class C : base { x = 1; y = 1;}

Is there a way to Pass class as parameter to method and access that class's variable value. SO, One function that can update values for all 3 classes.

Intention :

int call (type classtype)
{
   int xvalue = classtype.x;
   int yvalue = classtype.y;
}

I've seen in some posts a mention of activator.CreateInstance(classtype) in How to pass a Class as parameter for a method? [duplicate]

But it doesn't answer how we can access variables of that class.

Community
  • 1
  • 1
akshay dhule
  • 167
  • 3
  • 17
  • It should be `int call (base classtype)` and you would pass the instantiated object. – Legends Oct 20 '17 at 20:55
  • 4
    The *type* doesn't have those variables. *Instances* of that type have those variables, so having the *type* doesn't give you any variables. – Servy Oct 20 '17 at 20:55
  • Please show code for "classes A,B" - very unclear what you mean "class's variable value" as @Servy said. You may be referring to either static class fields/propertied *or* instance fields/properties - would be clear if code actually C# and not some pseudo-language. – Alexei Levenkov Oct 21 '17 at 00:12
  • @AlexeiLevenkov : well I know type doesnt give any variables. That's why I explained it as what are intentions to get in return. Also, A, B & C are nothing but extending same vars and initializing to let's A(1,0) B(0,1) & C(1,1). Now I am expecting update these initialized values runtime. I dont want to implement 3 functions for 3 classes so would like to know if we can simply to one generic for all 3. – akshay dhule Oct 24 '17 at 23:49

2 Answers2

1

Your method needs to accept the Type and then you can access static properties because you don't have the instance.

int Call(Type classType)
{
   var xvalue = (int)classType.GetProperty("x", BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
   var yvalue = (int)classType.GetProperty("y", BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
}

Although I have a feeling what you're really looking for is just simple inheritance or an interface as your parameter.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
1

You could change Call to accept the base class that A,B,C derives from:

int Call(base theClass)
{
    if (theClass is A)
    {
        var ax = theClass.x;
        var ay = theClass.y;
    }
    else if (theClass is B)
    {
        // etc       
    }
    // etc
}
mattshu
  • 96
  • 11
  • 2
    Usually when you find your self doing this it is a sign of code smell. Instead of having a bunch of `is` checks you just make two overloads `Call(A theClass)` and `Call(B theClass)` – Scott Chamberlain Oct 20 '17 at 21:22
  • 1
    The thing is functionality is same for A, B and C and it's not efficient to set different functions/process for each class, Instead we should be able to send a para and depending on that create/access instance of respective class. – akshay dhule Oct 20 '17 at 22:43