I read some article regarding expando object here but I want to achieve different thing.
I want to add property object with dynamic property at runtime, put value on it and then retrieve later:
private static readonly ConditionalWeakTable<object, ExpandoObject> props = new ConditionalWeakTable<object, ExpandoObject>();
public static void AddDataExtension(this object key, dynamic value)
{
props.Add(key, value);
}
public static dynamic GetDataExtension(this object key)
{
ExpandoObject ex = null;
return props.TryGetValue(key, out ex);
}
Usage:
'Insert data at runtime'
instance.AddDataExtension("Hello", "hi");
'Get the data at runtime'
instance.GetDataExtension("Hello")
But I receive this error:
The best overloaded method match for 'System.Runtime.CompilerServices.ConditionalWeakTable<object,System.Dynamic.ExpandoObject>.Add(object, System.Dynamic.ExpandoObject)' has some invalid arguments
I think I misused this property, is this possible to achieve? if yes, how? Please help.
Edit
here is the complete class:
public static class instance
{
private static readonly ConditionalWeakTable<object, ExpandoObject> props = new ConditionalWeakTable<object, ExpandoObject>();
public static void AddDataExtension(this object key, dynamic value)
{
props.Add(key, value);
}
public static dynamic GetDataExtension(this object key)
{
ExpandoObject ex = null;
return props.TryGetValue(key, out ex);
}
}
What I want to achieve is this:
I will have random varialbes, for example, "photo_01, photo_12, photo_15, name_01, name_02, age_01, age_02
"
If possible I want to use the method in this way:
id = <fetch from dbase>
instance.AddDataExtension("photo_" + id, byte[]);
And then retrieve the value:
instance.GetDataExtension("photo_" + id)