0

I need a way to provide a Dynamic get members and set for a given class. I want to be able to write code like this:

ns1.Resource.Field1 = "Hello";
string myField = ns1.Resource.Field1;

where ns1 is the namespace and I believe that "Resource" is the class name and Field1 or any other property is dynamic. So how do I declare a class like this ?

I've learned about inheriting Resource class from "DynamicObject" but its forcing me to instantiate the class Resource to an object, an operation I don't want to do.

Edit#1:
I want to create a way to use class like this:

Namespace.Resource.DynamicField = "Value";
string myValue = Namespace.Resource.DynamicField;

The "Resource" should not be instantiated and the DynamicField is a member that my class will be able to handle the get and set calls on it, so If at some place in code I write

Namespace.Resource.DynamicField2 = "Hello";

I will have a place where I can override the set call of to the static property "DynamicField2" of Resource. But I don't know in advanced the complete properties list of the class, So I need the properties to be dynamically created and be able to control the get and set like it was passed by "Name" let's say:

public class Resource{

   public static getMember(string Name){
       console.log(Name); //=> this will output "DynamicField2"
       return this.dictionary["Name"];
   }
}  

and then use it someplace at code

string a = Resource.DynamicField2; // a will be value "Hello" 
Haddar Macdasi
  • 3,477
  • 8
  • 37
  • 59
  • Are you saying that you'd also like the class name to be dynamic? So that you could put `ns1.Resource.xyz` and `ns1.Resource2.xyz` without declaring either of the classes? – Martin Nov 11 '18 at 10:54
  • I cannot understand your question, can you please clarify where is the dynamicly needed ? – Mohammad Ghanem Nov 11 '18 at 10:58
  • Also ExpandoObject (https://learn.microsoft.com/en-us/dotnet/api/system.dynamic.expandoobject?view=netframework-4.7.2) would be an option. But you will need to instantiate an object (could be a singleton, of course) in any case. Why is this not possible? – Klaus Gütter Nov 11 '18 at 11:04
  • @MartinParkin Yes. – Haddar Macdasi Nov 11 '18 at 11:10
  • How would the compiler know what type you are using, a dynamic namespace does not exist. Closest you will get is a `public static ExpandoObject Resource` – Lennart Stoop Nov 11 '18 at 11:13

2 Answers2

1

Take a look at ExpandoObject:

https://learn.microsoft.com/en-us/dotnet/api/system.dynamic.expandoobject?view=netframework-4.7.2

It should serve your needs.

EDIT.

You could create a static property in the Resource class to access a singleton instance of the ExpandoObject.

Eg

public static class Resource
{
     public static dynamic Data {get;} = new ExpandoObject();
}

Then simply set Resource.Data.Field1 = whatever; etc.

Yair Halberstadt
  • 5,733
  • 28
  • 60
0

I don't exactly understand what you mean . But if you want to have a value like that (without creating an object) . You can declare your class and var as static like this:

namespace ns1{

public static class Resource {

    public static string Field1 = "hello-f1";
    public static string Field2 = "hello-f2";
}}

after that you can use this variable by call that Note that the Fields variable is not const so you can change it everywhere

amir mehr
  • 75
  • 1
  • 9