0

我有一个字典,其中键是表示类属性名称的字符串,值是我要分配给该属性的值。我已经确定了 Dictionary 项目所引用的属性,但我不确定如何在不使用具有所有可能属性的荒谬 switch 语句的情况下实际设置该属性或调用适当的方法。有没有一些有效的方法来做到这一点?我无法更改 UserConfiguration 类或扩展它。

这是我到目前为止所拥有的:

public class Class1
{
    public Dictionary<string, string> PDictionary = new Dictionary<string, string>
        {
            {"UserName", "Steve"},
            {"Location", "Over There Somewhere"},
            {"Color", "Mellow Yellow"},
            {"Password", "ILikeCheese"}
        };

    public void SomeMethod()
    {
        foreach (var prop in PDictionary)
        {
            UserConfiguration.Property property;
            Enum.TryParse(prop.Key, out property);

            //Set appropriate property or call appropriate method...

        }
    }
}

public class UserConfiguration
{
    public enum Property
    {
        UserName,
        Location,
        Color,
        Password
    }

    public string UserName { get; set; }
    public string Location { get; set; }
    public string Color { get; set; }
    public string Password { get; private set; }

    public void SetPassword(string password)
    {
        Password = password;
    }
}

有人有什么好主意吗?

4

2 回答 2

2

把它放到你的课堂上:

public object this[string propertyName]
{
    get
    {
        Type myType = typeof(UserConfiguration);
        PropertyInfo myPropInfo = myType.GetProperty(propertyName);
        return myPropInfo.GetValue(this, null);
    }
    set
    {
        Type myType = typeof(UserConfiguration);
        PropertyInfo myPropInfo = myType.GetProperty(propertyName);
        myPropInfo.SetValue(this, value, null);
    }
}

然后您可以使用在类中获取/设置属性myClassInstance[property] = myValue;

于 2013-09-10T12:03:19.277 回答
1

假设您有一个方法,Class1其中有一个enum Property设置正确属性的参数,您可以像这样使用反射(对不起,我现在无法访问 VS 的任何编译错误)

Type tClass ;
Type paramType ;
MethodInfo[] methods ;
ParameterInfo[] params ;
Class1 c1 ;
UserConfiguration.Property property;

Enum.TryParse(prop.Key, out property);

Object []paramValues = {prop.value} ;

tClass = typeof(Class1) ;


c1 = new Class1() ;

bool invoked ;

invoked = false ;

//get all the methods of your Class1
methods = tClass.GetMethods() ;

foreach(MethodInfo mi in methods)
{
    //get the parameters of the current method
    params = mi.GetParameters() ;
    if(params != null)
    {
        foreach(ParameterInfo pi in params)
        {
            paramType = pi.ParameterType ;

            if(paramType = typeof(UserConfiguration.Property))
            {
                //a method that receives a userconfiguration properpty has been found
                //now you can call it
                mi.Invoke(c1, paramValues) ;
                invoked = true ;
                break ;
            }
        }
    }
    if(invoked)
    {
        break;
    }
}

如果您有名称如setUserName,的方法setLocation,那么您可以执行以下操作setColorsetPassword

Type tClass ;
Type paramType ;
MethodInfo[] methods ;
ParameterInfo[] params ;
Class1 c1 ;
UserConfiguration.Property property;

Enum.TryParse(prop.Key, out property);

Object []paramValues = {prop.Value} ;

tClass = typeof(Class1) ;


c1 = new Class1() ;

bool invoked ;

invoked = false ;

//get all the methods of your Class1

methods = tClass.GetMethods() ;
foreach(MethodInfo mi in methods)
{
    if(mi.Name.StartsWith("set" + prop.Key)
    {
        //a method name "set<PropertyName>" has been found
        //now you can call it
        mi.Invoke(c1, paramValues) ;
        break ;
    }
}

我建议你看一下 Type 类来决定哪种方法更适合你

http://msdn.microsoft.com/es-es/library/system.type.aspx

于 2013-09-10T12:21:44.080 回答