Reputation: 2026
public class UserDetails
{
public string UserID { get; set; }
public string UserName { get; set; }
}
Here i want to add property dynamically. The type and property name would change dynamically, with those value i want to create property.
Upvotes: 0
Views: 1482
Reputation: 14580
This seems to work but requires casting to get to the "flexible" properties.
The UserDetails
class
public class UserDetails
{
private dynamic _internal;
public static implicit operator System.Dynamic.ExpandoObject(UserDetails details)
{
return details._internal;
}
public UserDetails()
{
_internal = new System.Dynamic.ExpandoObject();
}
public string UserID
{
get
{
return _internal.UserID;
}
set
{
_internal.UserID = value;
}
}
public string UserName
{
get
{
return _internal.UserName;
}
set
{
_internal.UserName = value;
}
}
}
And using the class
UserDetails user = new UserDetails();
user.UserName = "bill";
user.UserID = "1";
dynamic dynamicUser = (System.Dynamic.ExpandoObject)user;
dynamicUser.newMember = "check this out!";
Console.WriteLine(user.UserName);
Console.WriteLine(user.UserID);
Console.WriteLine(dynamicUser.UserName);
Console.WriteLine(dynamicUser.UserID);
Console.WriteLine(dynamicUser.newMember);
Upvotes: 1
Reputation: 109537
It seems possible that all you really need is a "Property Bag", i.e. an unordered container into which you can insert name/value pairs where the name is a string and the value is any kind of object.
There are many implementations of PropertyBag available online; here's a quick and dirty one I threw together as an example:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
namespace Demo
{
public static class Program
{
private static void Main(string[] args)
{
var properties = new PropertyBag();
properties["Colour"] = Color.Red;
properties["π"] = Math.PI;
properties["UserId"] = "My User ID";
properties["UserName"] = "Matthew";
// Enumerate all properties.
foreach (var property in properties)
{
Console.WriteLine(property.Key + " = " + property.Value);
}
// Check if property exists:
if (properties["UserName"] != null)
{
Console.WriteLine("[UserName] exists.");
}
// Get a property:
double π = (double)properties["π"];
Console.WriteLine("Pi = " + π);
}
}
public sealed class PropertyBag: IEnumerable<KeyValuePair<string, object>>
{
public object this[string propertyName]
{
get
{
if (propertyName == null)
{
throw new ArgumentNullException("propertyName");
}
if (_dict.ContainsKey(propertyName))
{
return _dict[propertyName];
}
else
{
return null;
}
}
set
{
if (propertyName == null)
{
throw new ArgumentNullException("propertyName");
}
_dict[propertyName] = value;
}
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return _dict.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private readonly Dictionary<string, object> _dict = new Dictionary<string, object>();
}
}
Upvotes: 0
Reputation: 902
Yes, but it's complicated.
Check out implementing ICustomTypeDescriptor
. If you make your base class implement it, you will be able to add properties dynamically. There are tutorials on the web, search for the interface on the web.
The 2nd thing can be to use the ExpandoObject.
In this way you can not inherit from a base class, but it is much simpler to implement.
Upvotes: 0