Providing a DynamicObject properties











up vote
0
down vote

favorite
1












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" 









share|improve this question
























  • 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 Parkin
    Nov 11 at 10:54










  • I cannot understand your question, can you please clarify where is the dynamicly needed ?
    – Mohammad Alghanem
    Nov 11 at 10:58










  • Also ExpandoObject (docs.microsoft.com/en-us/dotnet/api/…) 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 at 11:04










  • @MartinParkin Yes.
    – Haddar Macdasi
    Nov 11 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 at 11:13















up vote
0
down vote

favorite
1












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" 









share|improve this question
























  • 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 Parkin
    Nov 11 at 10:54










  • I cannot understand your question, can you please clarify where is the dynamicly needed ?
    – Mohammad Alghanem
    Nov 11 at 10:58










  • Also ExpandoObject (docs.microsoft.com/en-us/dotnet/api/…) 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 at 11:04










  • @MartinParkin Yes.
    – Haddar Macdasi
    Nov 11 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 at 11:13













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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" 









share|improve this question















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" 






c# dynamicobject






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:01

























asked Nov 11 at 10:50









Haddar Macdasi

1,83142649




1,83142649












  • 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 Parkin
    Nov 11 at 10:54










  • I cannot understand your question, can you please clarify where is the dynamicly needed ?
    – Mohammad Alghanem
    Nov 11 at 10:58










  • Also ExpandoObject (docs.microsoft.com/en-us/dotnet/api/…) 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 at 11:04










  • @MartinParkin Yes.
    – Haddar Macdasi
    Nov 11 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 at 11:13


















  • 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 Parkin
    Nov 11 at 10:54










  • I cannot understand your question, can you please clarify where is the dynamicly needed ?
    – Mohammad Alghanem
    Nov 11 at 10:58










  • Also ExpandoObject (docs.microsoft.com/en-us/dotnet/api/…) 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 at 11:04










  • @MartinParkin Yes.
    – Haddar Macdasi
    Nov 11 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 at 11:13
















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 Parkin
Nov 11 at 10:54




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 Parkin
Nov 11 at 10:54












I cannot understand your question, can you please clarify where is the dynamicly needed ?
– Mohammad Alghanem
Nov 11 at 10:58




I cannot understand your question, can you please clarify where is the dynamicly needed ?
– Mohammad Alghanem
Nov 11 at 10:58












Also ExpandoObject (docs.microsoft.com/en-us/dotnet/api/…) 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 at 11:04




Also ExpandoObject (docs.microsoft.com/en-us/dotnet/api/…) 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 at 11:04












@MartinParkin Yes.
– Haddar Macdasi
Nov 11 at 11:10




@MartinParkin Yes.
– Haddar Macdasi
Nov 11 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 at 11:13




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 at 11:13












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Take a look at ExpandoObject:



https://docs.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.






share|improve this answer






























    up vote
    0
    down vote













    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






    share|improve this answer























    • That forces me to declare each member in advanced. I want members to be dynamically set and get.
      – Haddar Macdasi
      Nov 11 at 11:11











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247988%2fproviding-a-dynamicobject-properties%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Take a look at ExpandoObject:



    https://docs.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.






    share|improve this answer



























      up vote
      1
      down vote













      Take a look at ExpandoObject:



      https://docs.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.






      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        Take a look at ExpandoObject:



        https://docs.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.






        share|improve this answer














        Take a look at ExpandoObject:



        https://docs.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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 22:40

























        answered Nov 11 at 11:44









        Yair Halberstadt

        981222




        981222
























            up vote
            0
            down vote













            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






            share|improve this answer























            • That forces me to declare each member in advanced. I want members to be dynamically set and get.
              – Haddar Macdasi
              Nov 11 at 11:11















            up vote
            0
            down vote













            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






            share|improve this answer























            • That forces me to declare each member in advanced. I want members to be dynamically set and get.
              – Haddar Macdasi
              Nov 11 at 11:11













            up vote
            0
            down vote










            up vote
            0
            down vote









            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






            share|improve this answer














            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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 11:07

























            answered Nov 11 at 11:02









            amir mehr

            306




            306












            • That forces me to declare each member in advanced. I want members to be dynamically set and get.
              – Haddar Macdasi
              Nov 11 at 11:11


















            • That forces me to declare each member in advanced. I want members to be dynamically set and get.
              – Haddar Macdasi
              Nov 11 at 11:11
















            That forces me to declare each member in advanced. I want members to be dynamically set and get.
            – Haddar Macdasi
            Nov 11 at 11:11




            That forces me to declare each member in advanced. I want members to be dynamically set and get.
            – Haddar Macdasi
            Nov 11 at 11:11


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247988%2fproviding-a-dynamicobject-properties%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine