I've a dynamic object created using System.Dynamic.ExpandoObject(), now in some cases some properties could not exists, and if try to access to those in this way
myObject.undefinedProperties;
the default behavior of the object is to throw the exception
'System.Dynamic.ExpandoObject' does not contain a definition for 'undefinedProperties'
Is possible to change this behavior and return in that case the null value?
If you could replace ExpandoObject with DynamicObject, you could write own class that meets your requirements:
public class MyExpandoReplacement : DynamicObject
{
private Dictionary<string, object> _properties = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (!_properties.ContainsKey(binder.Name))
{
result = GetDefault(binder.ReturnType);
return true;
}
return _properties.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
this._properties[binder.Name] = value;
return true;
}
private static object GetDefault(Type type)
{
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}
}
Usage:
dynamic a = new MyExpandoReplacement();
a.Sample = "a";
string samp = a.Sample; // "a"
string samp2 = a.Sample2; // null
ExpandoObject inherits IDictionary <string, object> so you can check if the object has "undefinedProperties" like this
if (((IDictionary<string, object>)myObject).ContainsKey("undefinedProperties"))
{
// Do something
}
You can test the existence of property in the ExpandoObject, see here Detect property in ExpandoObject
Related
I define a DynamicObject.
I have created a list of DynamicObjects with the same structure and link them to a WPF GridView.
I allow editing of some of the properties via the grid.
As the DynamicObjects present the property data as objects, how can I enforce Type restrictions?
if the user types alphabet into a cell that I would like as an int how can I get the DynamicObject to refuse the input?
You could use a TryParse wherever you're taking the cell input:
int result;
if(int.TryParse(cellText, out result))
{
// Is an integer
}
else
{
}
bool and other value types also have a TryParse if you're taking those values as well.
See also:
Comparing Types in this question
The DynamicDictionary example in the docs for a more verbose implementation on adding and editing properties.
In the constructor of my DynamicObject, I pass in with the properties definition, a dictionary of the types.
I then override the TrySetMember method to convert the value from the string supplied by the grid into its required type.
The issue I now have is sending a error message back to the grid if the conversion fails.
Here is My DynamicObject definition:
public sealed class FwDynamicObject : DynamicObject
{
private readonly Dictionary<string, object> _properties;
private readonly Dictionary<string, Type> _propertyTypes;
public FwDynamicObject(Dictionary<string, object> properties, Dictionary<string, Type> propertyTypes = null)
{
_properties = properties;
_propertyTypes = propertyTypes;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _properties.Keys;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (_properties.ContainsKey(binder.Name))
{
result = _properties[binder.Name];
return true;
}
else
{
result = null;
return false;
}
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (_properties.ContainsKey(binder.Name))
{
var t = GetMemberType(binder.Name);
if (t != null)
{
try
{
value = Convert.ChangeType(value, t);
}
catch(Exception e)
{
return false;
}
}
_properties[binder.Name] = value;
return true;
}
else
{
return false;
}
}
private Type GetMemberType(string name)
{
if (_propertyTypes.ContainsKey(name))
{
return _propertyTypes[name];
}
return null;
}
}
I have a class Example which contains a dictionary object Dict and as such I can access a dictionary value with key "X" using Example.Dict["X"].
However, I'm wondering if it's possible to carry out these accesses as follows: Example.X.
I would usually handle this using a basic compiler macro in C or C++ but seeing as C# doesn't have this feature I'm wondering if it is possible via reflection or otherwise.
You can do this with DynamicObject:
public class Example : DynamicObject
{
public Dictionary<string, string> Dict { get; } = new Dictionary<string, string>() { ["Foo"] = "bar" };
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null;
if (!Dict.TryGetValue(binder.Name, out var value))
return false;
result = value;
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (Dict.ContainsKey(binder.Name))
{
Dict[binder.Name] = value?.ToString();
return true;
}
return Dict.TryAdd(binder.Name, value?.ToString());
}
}
An an example above I have initialised the Dictionary<string,string> with a default element. This allows me to write code such as:
dynamic ex = new Example();
Console.WriteLine(ex.Foo); // Outputs "bar"
ex.SomethingElse = "SomeValue"; // sets an element in the dictionary
Whether this is a good idea is an exercise for the reader.
So I've created a class that inherits DynamicObject
public class MyDynamicObject : DynamicObject{
private Dictionary<string, object> Fields = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return Fields.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
Fields[binder.Name] = value;
return true;
}
}
And call this class here
public class Program
{
public static void Main()
{
dynamic person = new MyDynamicObject();
person.firstname = "Hello";
Console.WriteLine(person.firstname);
}
}
Of course this will work. But I need to create properties from a string array like
string[] fields = new string[]{"taxid","newcol","addrs","gender"};
dynamic person = new MyDynamicObject();
foreach(var f in fields)
{
person.f = "hello";
}
So the output will be person.taxi, person.newcol, person.addrs, person.gender
Is this possible?
Expose the Fields dictionary in some way, or (better) a method that allows one to explicitly set a property by name.
Note that ExpandoObject already does this, as it can be cast to IDictionary<string, object> and then you
ExpandoObject eo = new ExpandoObject();
IDictionary<string, object> dict = eo;
dynamic d = eo;
dict["MyProperty"] = 42;
Console.WriteLine(d.MyProperty); // 42
If you can't just use ExpandoObject itself, you can copy its approach.
Okay so based on the suggestion of #Jon Hanna, I came up with a solution that fits my requirements. I created a new Add method which accept a name. Below is the updated code I used.
public class DynamicFormData : DynamicObject
{
private Dictionary<string, object> Fields = new Dictionary<string, object>();
public int Count { get { return Fields.Keys.Count; } }
public void Add(string name, string val = null)
{
if (!Fields.ContainsKey(name))
{
Fields.Add(name, val);
}
else
{
Fields[name] = val;
}
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (Fields.ContainsKey(binder.Name))
{
result = Fields[binder.Name];
return true;
}
return base.TryGetMember(binder, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (!Fields.ContainsKey(binder.Name))
{
Fields.Add(binder.Name, value);
}
else
{
Fields[binder.Name] = value;
}
return true;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (Fields.ContainsKey(binder.Name) &&
Fields[binder.Name] is Delegate)
{
Delegate del = Fields[binder.Name] as Delegate;
result = del.DynamicInvoke(args);
return true;
}
return base.TryInvokeMember(binder, args, out result);
}
}
Then I just call it like this.
string[] fields = new string[]{"taxid","newcol","addrs","gender"};
dynamic formData = new DynamicFormData();
foreach(string field in fields)
{
formData.Add(field, null);
}
Is there a way to access a NameValueCollection (like Request.Form or ConfigurationManager.AppSettings) as a dynamic object?
I'd like to be able to do something like this:
var settings = ConfigurationManager.AppSettings.AsDynamic();
var name = settings.Name; // ConfigurationManger.AppSettings["Name"]
// but also
settings.Name = "Jones"; // modify the original collection
// and
var form = Request.Form.AsDynamic();
var email = form.Email; // Request.Form["Email"]
(this question is based on Convert a NameValueCollection to a dynamic object )
You could write an adapter that wraps your NameValueCollection and that inherits from DynamicObject. You could then create an extension method that instantiates the adapter. To finish it off, you could make your wrapper class implicitly castable to you original NameValueCollection, so you could use the wrapped collection everywhere you would be able to use the original collection:
public static class Utility
{
public static dynamic AsDynamic(this NameValueCollection collection)
{
return (NameValueCollectionDynamicAdapter)collection;
}
private class NameValueCollectionDynamicAdapter : DynamicObject
{
private NameValueCollection collection;
public NameValueCollectionDynamicAdapter(NameValueCollection collection)
{
this.collection = collection ?? throw new NullReferenceException(nameof(collection));
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = collection[binder.Name];
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
collection[binder.Name] = value?.ToString();
return true;
}
public static implicit operator NameValueCollection(NameValueCollectionDynamicAdapter target)
{
return target.collection;
}
public static explicit operator NameValueCollectionDynamicAdapter(NameValueCollection collection)
{
return new NameValueCollectionDynamicAdapter(collection);
}
}
}
I'm talking about something similar to dynamic. This didn't answer my question, hence this question. I want to have a class that I can add properties to at runtime. It needs to be inherited from the type object.
I've seen inheriting from DynamicObject, but it didn't state how to add properties at run-time. Could some light be shed on this for me pls?
I have a class like this:
public class SomeModel : DynamicObject {
public string SomeMandatoryProperty {get; set;}
}
I'd like to add all properties from another class to this class at runtime. So eg.
SomeModel m = new SomeModel();
m = someOtherClass;
string hi = m.otherClassProp; //Property from other class is added.
string mandatory = m.SomeMandatoryProperty; //From the mandatory property set previously.
I think you are looking for ExpandoObject:
The ExpandoObject class enables you to
add and delete members of its
instances at run time and also to set
and get values of these members. This
class supports dynamic binding, which
enables you to use standard syntax
like sampleObject.sampleMember instead
of more complex syntax like
sampleObject.GetAttribute("sampleMember").
dynamic manager;
manager = new ExpandoObject();
manager.Name = "Allison Brown";
manager.Age = 42;
manager.TeamSize = 10;
You should be able to make use of ExpandoObject instead. An ExpandoObject can have members added or removed at runtime and has very nice support if you want to convert to XML etc.
From MSDN Documentation:
dynamic employee, manager;
employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;
manager = new ExpandoObject();
manager.Name = "Allison Brown";
manager.Age = 42;
manager.TeamSize = 10;
You'd want to use the ExpandoObject as you can dynamically add properties as needed. There isn't however a direct way to populate an instance with the values from another object easily. You'll have to add it manually using reflection.
Do you want to write a wrapper object where you could add properties to while still accessing the inner? You may want to consider it that way you don't have to manage two copies of values between two different object instances. I wrote a test class to wrap string objects to demonstrate how you can do this (similar to how the ExpandoObject works). It should give you an idea on how you can do this for your types.
class DynamicString : DynamicObject
{
static readonly Type strType = typeof(string);
private string instance;
private Dictionary<string, object> dynProperties;
public DynamicString(string instance)
{
this.instance = instance;
dynProperties = new Dictionary<string, object>();
}
public string GetPrefixString(string prefix)
{
return String.Concat(prefix, instance);
}
public string GetSuffixString(string suffix)
{
return String.Concat(instance, suffix);
}
public override string ToString()
{
return instance;
}
public override bool TryConvert(ConvertBinder binder, out object result)
{
if (binder.Type != typeof(string))
return base.TryConvert(binder, out result);
result = instance;
return true;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var method = strType.GetMethod(binder.Name, args.Select(a => a.GetType()).ToArray());
if (method == null)
{
result = null;
return false;
}
result = method.Invoke(instance, args);
return true;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var members = strType.GetMember(binder.Name);
if (members.Length > 0)
{
var member = members.Single();
switch (member.MemberType)
{
case MemberTypes.Property:
result = ((PropertyInfo)member).GetValue(instance, null);
return true;
break;
case MemberTypes.Field:
result = ((FieldInfo)member).GetValue(instance);
return true;
break;
}
}
return dynProperties.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
var ret = base.TrySetMember(binder, value);
if (ret) return true;
dynProperties[binder.Name] = value;
return true;
}
}
If you want to be adventurous, you can define your own meta objects to handle the bindings. You could end up with reusable meta objects for different types and simplify your code immensely. I've been playing with this for a while and have this so far. It doesn't handle dynamically adding properties yet. I won't be working on this any further but I'll just leave it here for reference.
class DynamicString : DynamicObject
{
class DynamicStringMetaObject : DynamicMetaObject
{
public DynamicStringMetaObject(Expression parameter, object value)
: base(parameter, BindingRestrictions.Empty, value)
{
}
public override DynamicMetaObject BindConvert(ConvertBinder binder)
{
if (binder.Type == typeof(string))
{
var valueType = Value.GetType();
return new DynamicMetaObject(
Expression.MakeMemberAccess(
Expression.Convert(Expression, valueType),
valueType.GetProperty("Instance")),
BindingRestrictions.GetTypeRestriction(Expression, valueType));
}
return base.BindConvert(binder);
}
public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
{
System.Diagnostics.Trace.WriteLine(String.Format("BindGetMember: {0}", binder.Name));
var valueType = Value.GetType();
var self = Expression.Convert(Expression, valueType);
var valueMembers = valueType.GetMember(binder.Name);
if (valueMembers.Length > 0)
{
return BindGetMember(self, valueMembers.Single());
}
var members = typeof(string).GetMember(binder.Name);
if (members.Length > 0)
{
var instance =
Expression.MakeMemberAccess(
self,
valueType.GetProperty("Instance"));
return BindGetMember(instance, members.Single());
}
return base.BindGetMember(binder);
}
private DynamicMetaObject BindGetMember(Expression instance, MemberInfo member)
{
return new DynamicMetaObject(
Expression.Convert(
Expression.MakeMemberAccess(instance, member),
typeof(object)),
BindingRestrictions.GetTypeRestriction(Expression, Value.GetType())
);
}
}
public string Instance { get; private set; }
public DynamicString(string instance)
{
Instance = instance;
}
public override DynamicMetaObject GetMetaObject(Expression parameter)
{
return new DynamicStringMetaObject(parameter, this);
}
public override string ToString()
{
return Instance;
}
public string GetPrefixString(string prefix)
{
return String.Concat(prefix, Instance);
}
public string GetSuffixString(string suffix)
{
return String.Concat(Instance, suffix);
}
}