Providing a DynamicObject properties
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
Are you saying that you'd also like the class name to be dynamic? So that you could putns1.Resource.xyz
andns1.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 apublic static ExpandoObject Resource
– Lennart Stoop
Nov 11 at 11:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
c# dynamicobject
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 putns1.Resource.xyz
andns1.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 apublic static ExpandoObject Resource
– Lennart Stoop
Nov 11 at 11:13
add a comment |
Are you saying that you'd also like the class name to be dynamic? So that you could putns1.Resource.xyz
andns1.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 apublic 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
add a comment |
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.
add a comment |
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
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 12 at 22:40
answered Nov 11 at 11:44
Yair Halberstadt
981222
981222
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Are you saying that you'd also like the class name to be dynamic? So that you could put
ns1.Resource.xyz
andns1.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