1

Say I have 2 classes:

Class 1:

public class Class1
{
    public static string Test = "Class1";
}

And Class 2:

public class Class2
{
    public static string Test = "Class2";
}

Now in my mainform, I have an array of objects:

object[] Objects = new object[2];
Objects[0] = new Class1();
Objects[1] = new Class2();

Now say I would want to get the Test string from object[0] How would I retrieve that?

I have tried

Objects[0].GetType().GetProperty("Test");  //Returns a NullReference
4
  • 4
    Cast it to Class1 or use inheritance and polymorphism. Let both classes inherit the same base class or implement the same interface which has a property Test. Then create an array of this class/interface and you can access this property without casting. Commented Mar 10, 2016 at 12:50
  • Class1.Test and Class2.Test? They are static properties, you dont need object to access them. You can access them directly with the class names. Commented Mar 10, 2016 at 12:53
  • 1
    @TimSchmelter The Test are static, so no inheritance/polymorphism... Commented Mar 10, 2016 at 12:54
  • 1
    @xanatos: true and they are fields. But this is just an example. Maybe they're accidentially static. I just wanted to give OP another way of looking at things. Reflection should be the very last resort and is much too often abused to solve an issue which should not exist if you've understood the concepts of OOP. Also, using Object as type should not be needed often nowadays. Commented Mar 10, 2016 at 12:56

3 Answers 3

2

First off, you cannot call anything declared static on a instance. And secondly this is exactly what polymorphism is for.

public interface IClass
{
    string Test { get; }
}

public class Class1 : IClass
{
    public string Test
    {
        get { return "Class1"; }
    }
}

public class Class2 : IClass
{
    public string Test
    {
        get { return "Class2"; }
    }
}


IClass[] Objects = new IClass[2];
Objects[0] = new Class1();
Objects[1] = new Class2();

Then you can just do.

var str = Objects[0].Test;
Sign up to request clarification or add additional context in comments.

2 Comments

Either that (base class) or an use an interface, depending on how your two objects are related.
Agree, in this given scenario an interface would be more appropriate.
2

You can do

if (object[0] is Class1) { ... }
if (object[1] is Class2) { ... }

such as

Console.WriteLine(Class1.Test );

So you could do if (Objects[0] is Class1) Console.WriteLine(Class1.Text); as a whole piece of code.

2 Comments

No. It cannot access static field Test in non-static context. Because Test is static in non-static class!
Corrected for clarity
0
Objects[0].GetType().GetField("Test").GetValue(null);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.