setting class members automatically like Photon Servers Operation class does

I’m wanting to mimic something that I saw in Photon Server with my application. Entity Framework does something similar. Photon Server has an Operation class. You pass it a dictionary of <byte,object> and it auto fills the class members and also does some data annotation. For example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class MyCustomOperation : Operation
{
public MyCustomOperation(IRpcProtocol protocol, OperationRequest request) : base(protocol, request)
{
}
[DataMember(Code = 100, IsOptional = false)]
public string Message { get; set; }
}
</code>
<code>public class MyCustomOperation : Operation { public MyCustomOperation(IRpcProtocol protocol, OperationRequest request) : base(protocol, request) { } [DataMember(Code = 100, IsOptional = false)] public string Message { get; set; } } </code>
public class MyCustomOperation : Operation
{
    public MyCustomOperation(IRpcProtocol protocol, OperationRequest request) : base(protocol, request)
    {
    }
    [DataMember(Code = 100, IsOptional = false)]
    public string Message { get; set; }
}

Now in this, request contains a Dictionary<byte,object> the byte is what the Data annotation refers to as the Code it somehow takes that dictionary looks up the key that matches what code is set to and assigns Message to the value of the key.

I need to do the same thing and have had no luck finding anything on google (although not sure what this is called) I know the [DataMember] line is a custom data annotation and those I do understand its the part of taking the code in the data annotation and assigning the value to the field under it.

What I know about custom data annotations is using validation with them (which this class also does in Photon via a MyCustomOperation.IsValid although this part I can figure out I think).

Ok I finally figured this out without using reflection as well

DataMemberAttribute class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class DataMemberAttribute : Attribute
{
public byte Code { get; set; }
public bool IsOptional { get; set; }
public string ErrorMessage { get; set; }
}
</code>
<code>[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class DataMemberAttribute : Attribute { public byte Code { get; set; } public bool IsOptional { get; set; } public string ErrorMessage { get; set; } } </code>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class DataMemberAttribute : Attribute
{
    public byte Code { get; set; }
    public bool IsOptional { get; set; }
    public string ErrorMessage { get; set; }
}

Operation Class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Operation
{
private readonly List<string> _errorMessages;
protected Operation(Dictionary<byte,object> parameters)
{
_errorMessages = new List<string>();
foreach (var prop in GetType().GetProperties())
{
var da = prop.GetCustomAttributes(typeof(DataMemberAttribute), false) as DataMemberAttribute[];
if (!(da == null || da.Length <= 0))
{
if (parameters.ContainsKey(da[0].Code))
{
prop.SetValue(this, Convert.ChangeType(parameters[da[0].Code], prop.PropertyType), null);
}
else
{
_errorMessages.Add(!string.IsNullOrEmpty(da[0].ErrorMessage) ? da[0].ErrorMessage : string.Format("{0} is Required", prop.Name));
}
}
}
}
public bool IsValid
{
get {
return _errorMessages.Count <= 0;
}
}
public List<string> ErrorMessages
{
get { return _errorMessages; }
}
}
</code>
<code>public class Operation { private readonly List<string> _errorMessages; protected Operation(Dictionary<byte,object> parameters) { _errorMessages = new List<string>(); foreach (var prop in GetType().GetProperties()) { var da = prop.GetCustomAttributes(typeof(DataMemberAttribute), false) as DataMemberAttribute[]; if (!(da == null || da.Length <= 0)) { if (parameters.ContainsKey(da[0].Code)) { prop.SetValue(this, Convert.ChangeType(parameters[da[0].Code], prop.PropertyType), null); } else { _errorMessages.Add(!string.IsNullOrEmpty(da[0].ErrorMessage) ? da[0].ErrorMessage : string.Format("{0} is Required", prop.Name)); } } } } public bool IsValid { get { return _errorMessages.Count <= 0; } } public List<string> ErrorMessages { get { return _errorMessages; } } } </code>
public class Operation
{
    private readonly List<string> _errorMessages; 
    protected Operation(Dictionary<byte,object> parameters)
    {
        _errorMessages = new List<string>();
        foreach (var prop in GetType().GetProperties())
        {
            var da = prop.GetCustomAttributes(typeof(DataMemberAttribute), false) as DataMemberAttribute[];
            if (!(da == null || da.Length <= 0))
            {
                if (parameters.ContainsKey(da[0].Code))
                {
                    prop.SetValue(this, Convert.ChangeType(parameters[da[0].Code], prop.PropertyType), null);
                }
                else
                {
                    _errorMessages.Add(!string.IsNullOrEmpty(da[0].ErrorMessage) ? da[0].ErrorMessage : string.Format("{0} is Required", prop.Name));
                }
            }
        }
    }

    public bool IsValid
    {
        get {
            return _errorMessages.Count <= 0;
        }
    }

    public List<string> ErrorMessages
    {
        get { return _errorMessages; }
    } 
}

Sample Derived class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Operation2 : Operation
{
public Operation2(Dictionary<byte,object> parameters) : base(parameters)
{}
[DataMember(Code = 1, IsOptional = false, ErrorMessage="Username is Required")]
public string Username { get; set; }
[DataMember(Code = 2, IsOptional = false, ErrorMessage="Password is Required"))]
public string Password { get; set; }
[DataMember(Code = 3, IsOptional = false, ErrorMessage="Email is Required"))]
public string Email { get; set; }
}
</code>
<code>public class Operation2 : Operation { public Operation2(Dictionary<byte,object> parameters) : base(parameters) {} [DataMember(Code = 1, IsOptional = false, ErrorMessage="Username is Required")] public string Username { get; set; } [DataMember(Code = 2, IsOptional = false, ErrorMessage="Password is Required"))] public string Password { get; set; } [DataMember(Code = 3, IsOptional = false, ErrorMessage="Email is Required"))] public string Email { get; set; } } </code>
public class Operation2 : Operation
{
    public Operation2(Dictionary<byte,object> parameters) : base(parameters)
    {}

    [DataMember(Code = 1, IsOptional = false, ErrorMessage="Username is Required")]
    public string Username { get; set; }

    [DataMember(Code = 2, IsOptional = false, ErrorMessage="Password is Required"))]
    public string Password { get; set; }

    [DataMember(Code = 3, IsOptional = false, ErrorMessage="Email is Required"))]
    public string Email { get; set; } 

}

Test Program:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Program
{
static void Main()
{
var paramters = new Dictionary<byte, object> {{1, "someuser"}, {2, "password"}, {3, "[email protected]"}};
var operation = new Operation2(paramters);
var paramters2 = new Dictionary<byte, object> { { 1, "someuser" }, { 10, "password" }, { 3, "[email protected]" } };
var operation2 = new Operation2(paramters2);
Console.WriteLine("Operation 1 Contains:");
Console.WriteLine("Username {0}", operation.Username);
Console.WriteLine("Email {0}", operation.Email);
Console.WriteLine("Password {0}", operation.Password);
Console.WriteLine("operation 1 {0}", operation.IsValid);
Console.WriteLine();
Console.WriteLine("Operation 2 Contains:");
Console.WriteLine("Username {0}", operation2.Username);
Console.WriteLine("Email {0}", operation2.Email);
Console.WriteLine("Password {0}", operation2.Password);
Console.WriteLine("operation 2 {0}", operation2.IsValid);
Console.ReadLine();
}
}
</code>
<code>class Program { static void Main() { var paramters = new Dictionary<byte, object> {{1, "someuser"}, {2, "password"}, {3, "[email protected]"}}; var operation = new Operation2(paramters); var paramters2 = new Dictionary<byte, object> { { 1, "someuser" }, { 10, "password" }, { 3, "[email protected]" } }; var operation2 = new Operation2(paramters2); Console.WriteLine("Operation 1 Contains:"); Console.WriteLine("Username {0}", operation.Username); Console.WriteLine("Email {0}", operation.Email); Console.WriteLine("Password {0}", operation.Password); Console.WriteLine("operation 1 {0}", operation.IsValid); Console.WriteLine(); Console.WriteLine("Operation 2 Contains:"); Console.WriteLine("Username {0}", operation2.Username); Console.WriteLine("Email {0}", operation2.Email); Console.WriteLine("Password {0}", operation2.Password); Console.WriteLine("operation 2 {0}", operation2.IsValid); Console.ReadLine(); } } </code>
class Program
{
    static void Main()
    {
        var paramters = new Dictionary<byte, object> {{1, "someuser"}, {2, "password"}, {3, "[email protected]"}};
        var operation = new Operation2(paramters);
        var paramters2 = new Dictionary<byte, object> { { 1, "someuser" }, { 10, "password" }, { 3, "[email protected]" } };
        var operation2 = new Operation2(paramters2);

        Console.WriteLine("Operation 1 Contains:");
        Console.WriteLine("Username {0}", operation.Username);
        Console.WriteLine("Email {0}", operation.Email);
        Console.WriteLine("Password {0}", operation.Password);
        Console.WriteLine("operation 1 {0}", operation.IsValid);
        Console.WriteLine();
        Console.WriteLine("Operation 2 Contains:");
        Console.WriteLine("Username {0}", operation2.Username);
        Console.WriteLine("Email {0}", operation2.Email);
        Console.WriteLine("Password {0}", operation2.Password);
        Console.WriteLine("operation 2 {0}", operation2.IsValid);
        Console.ReadLine();
    }
}

Produces the following output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Operation 1 Contains:
Username someuser
Email [email protected]
Password password
operation 1 True
Operation 2 Contains:
Username someuser
Email [email protected]
Password
operation 2 False
</code>
<code>Operation 1 Contains: Username someuser Email [email protected] Password password operation 1 True Operation 2 Contains: Username someuser Email [email protected] Password operation 2 False </code>
Operation 1 Contains:
Username someuser
Email [email protected]
Password password
operation 1 True

Operation 2 Contains:
Username someuser
Email [email protected]
Password
operation 2 False

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật