📜  set-variables-from-an-object-using-reflection - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:44.001000             🧑  作者: Mango

代码示例2
public void Validate(object o)
{
    Type t = o.GetType();
    foreach (var prop in
        t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
    {
        if (Attribute.IsDefined(prop, typeof(RequiredAttribute)))
        {
            object value = prop.GetValue(o, null);
            if (value == null)
                throw new RequiredFieldException(prop.Name);
        }
    }
}