0

How can i call and then use (set value) for member of the class, when i do not know class name?

For best understanding let me show example, what i mean:

for example i have class:

static class ClassA {
    public static string Property1;
    public static bool Property2;
    public static async Task<bool> Method () {
     // ... 
     await Worker.Do();
     return true;
    } 
}

static class Worked {
   public static async Task<bool> Do() {
     // ... 
     var CallerClassName = new StackTrace().GetFrame(1).GetMethod().ReflectedType.Name;

     // setup value for caller class Property 1 = "test";
     // Like ClassA.Property1 = "test"; but using dynamic name;
     // setup value for caller class Property 2 = true;
     // Like ClassA.Property2 = true; but using dynamic name;
   }
}

i should say, that i know that those properties exists in Caller class, i do not know how to set up their name dynamicly if i do not know its class caller name. Also i should say that all this methods, property and classes are static.

Thanks for help!

Ony
  • 399
  • 6
  • 16

1 Answers1

0

don't do this. | your classes should not be static.if they have the same meothds/properties they should inherit from the same interface.

if you really insist you can use reflection Set object property using reflection

Community
  • 1
  • 1
Nahum
  • 6,959
  • 12
  • 48
  • 69
  • How is the first paragraph relevant? The OP is trying to use reflection to set the value of static properties in a certain class. What makes you think there are identical methods/properties? – Asad Saeeduddin Dec 28 '14 at 07:01
  • @Asad his question is not clear enough. but I have a feeling he is doing something wrong. trying to do something that can be solved by a common interface in a weird way. – Nahum Dec 28 '14 at 07:05