Возможно ли в C # получить доступ к полям объекта, используя имена полей, сгенерированные во время выполнения - PullRequest
3 голосов
/ 30 октября 2009

Вот что я имею в виду:

Мне нужно заменить этот уродливый код C #:

if (attribute.Name == "Name") machinePool.Name = attribute.Value;
else if (attribute.Name == "Capabilities") machinePool.Capabilities = attribute.Value;
else if (attribute.Name == "FillFactor") machinePool.FillFactor = attribute.Value;

примерно так:

machinePool.ConvertStringToObjectField(attribute.Name) = attribute.Value;

Нет метода ConvertStringToObjectField (), но я хотел бы иметь что-то подобное, если это возможно. У меня есть доступ к коду класса объекта machinePool, так что я могу добавить необходимый код, но я не уверен, какой это может быть код или вообще возможно ли это сделать в C #.

Ответы [ 3 ]

9 голосов
/ 30 октября 2009

Да, вы можете сделать это с помощью отражения:

var fieldInfo = machinePool.GetType().GetField(attribute.Name);
fieldInfo.SetValue(machinePool, attribute.Value);

Вы также можете создать метод расширения, чтобы упростить задачу:

public static void SetField(this object o, string fieldName, object value)
{
    var fi = o.GetType().GetField(fieldName);
    fi.SetValue(o, value);
}
0 голосов
/ 30 октября 2009

Вы можете сделать что-то вроде:

void SetPropertyToValue(string propertyName, object value)
{
    Type type = this.GetType();
    type.GetProperty(propertyName).SetValue(this, value, null);
}

Тогда используйте это как:

machinePool.SetPropertyToValue(attribute.Name, attribute.Value);
0 голосов
/ 30 октября 2009

Да, вы можете. Вот код, который я использую, когда хочу получить имена свойств в виде строк (т. Е. Для обработки PropertyChangedEventHandler), но я не хочу использовать сами строки:

    public static string GetPropertyName<T>(Expression<Func<T, object>> propertyExpression)
    {
        Check.RequireNotNull<object>(propertyExpression, "propertyExpression");
        switch (propertyExpression.Body.NodeType)
        {
            case ExpressionType.MemberAccess:
                return (propertyExpression.Body as MemberExpression).Member.Name;
            case ExpressionType.Convert:
                return ((propertyExpression.Body as UnaryExpression).Operand as MemberExpression).Member.Name;
        }
        var msg = string.Format("Expression NodeType: '{0}' does not refer to a property and is therefore not supported", 
            propertyExpression.Body.NodeType);
        Check.Require(false, msg);
        throw new InvalidOperationException(msg);
    }

А вот несколько тестовых кодов, которые помогут вам в работе:

[TestFixture]
public class ExpressionsExTests
{
    class NumbNut
    {
        public const string Name = "blah";
        public static bool Surname { get { return false; } }
        public string Lame;
        public readonly List<object> SerendipityCollection = new List<object>();
        public static int Age { get { return 12; }}
        public static bool IsMammel { get { return _isMammal; } }
        private const bool _isMammal = true;
        internal static string BiteMe() { return "bitten"; }
    }

    [Test]
    public void NodeTypeIs_Convert_aka_UnaryExpression_Ok()
    {
        Assert.That(ExpressionsEx.GetPropertyName<NumbNut>(nn => NumbNut.Age), Is.EqualTo("Age"));
        Assert.That(ExpressionsEx.GetPropertyName<NumbNut>(nn => NumbNut.IsMammel), Is.EqualTo("IsMammel"));
        Assert.That(ExpressionsEx.GetPropertyName<NumbNut>(nn => NumbNut.Surname), Is.EqualTo("Surname"));
    }

    [Test]
    public void NodeTypeIs_MemberAccess_aka_MemberExpression_Ok()
    {
        Assert.That(ExpressionsEx.GetPropertyName<NumbNut>(nn => nn.SerendipityCollection), Is.EqualTo("SerendipityCollection"));
        Assert.That(ExpressionsEx.GetPropertyName<NumbNut>(nn => nn.Lame), Is.EqualTo("Lame"));
    }

    [Test]
    public void NodeTypeIs_Call_Error()
    {
        CommonAssertions.PreconditionCheck(() => ExpressionsEx.GetPropertyName<NumbNut>(nn => NumbNut.BiteMe()),
                                           "does not refer to a property and is therefore not supported");
    }

    [Test]
    public void NodeTypeIs_Constant_Error() {
        CommonAssertions.PreconditionCheck(() => ExpressionsEx.GetPropertyName<NumbNut>(nn => NumbNut.Name),
                                           "does not refer to a property and is therefore not supported");
    }

    [Test]
    public void IfExpressionIsNull_Error()
    {
        CommonAssertions.NotNullRequired(() => ExpressionsEx.GetPropertyName<NumbNut>(null));
    }

    [Test]
    public void WasPropertyChanged_IfPassedNameIsSameAsNameOfPassedExpressionMember_True()
    {
        Assert.That(ExpressionsEx.WasPropertyChanged<NumbNut>("SerendipityCollection", nn => nn.SerendipityCollection), Is.True);
    }

    [Test]
    public void WasPropertyChanged_IfPassedPropertyChangeArgNameIsSameAsNameOfPassedExpressionMember_True()
    {
        var args = new PropertyChangedEventArgs("SerendipityCollection");
        Assert.That(ExpressionsEx.WasPropertyChanged<NumbNut>(args, nn => nn.SerendipityCollection), Is.True);
    }

}

НТН

Berryl

...