Mapping Properties to String Keys

Introduction

PropertyMapper<T> 是一个帮助类,允许映射对象属性到string keys.这也允许把新的值赋值给这些string keys.

开始写这些代码是用在EntityFramework Code-First.我也一个带有很多大写名称或下划线的数据列的表结构, 我不希望重新定义表结构 ,所以我决定写一个帮助类把对象属性映射到string keys, 从对象实例中分离值 并且快速映射回原有的original string keys,格式化成字典。

Source Code

public sealed class PropertyMapper<T>
{
    private readonly Dictionary<string, string> m_maps = new Dictionary<string, string>();
    private readonly Dictionary<string, string> m_props = new Dictionary<string, string>();

    private static string GetPropertyName<TReturn>(Expression<Func<T, TReturn>> property)
    {
        var expr = property.Body as MemberExpression;
        if (expr == null)
            throw new InvalidOperationException("Invalid property type");
        return expr.Member.Name;
    }

    public void MapProperty<TReturn>(Expression<Func<T, TReturn>> property, string mapTo)
    {
        var propName = GetPropertyName(property);
        m_maps[mapTo] = propName;
        m_props[propName] = mapTo;
    }

    public string GetPropertyMap<TReturn>(Expression<Func<T, TReturn>> property)
    {
        return GetPropertyMap(GetPropertyName(property));
    }

    public string GetPropertyMap(string property)
    {
        string result;
        return m_props.TryGetValue(property, out result) ? result : null;
    }

    public object GetPropertyValue(T instance, string property)
    {
        string result;
        if (!m_maps.TryGetValue(property, out result))
            return null;
        return typeof (T).GetProperty(result).GetValue(instance, null);
    }

    public IDictionary<string, object> GetPropertyValues(T instance)
    {
        return m_maps
            .Select(x => new {Name = x.Key, Value = typeof (T).GetProperty(x.Value).GetValue(instance, null)})
            .Where(x => x.Name != null && x.Value != null)
            .ToDictionary(x => x.Name, x => x.Value);
    }
}

Using the Code

定义类:
public sealed class Test
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

使用:
var mapper = new PropertyMapper<Test>();

mapper.MapProperty(x => x.Foo, "PROP-FOO");
mapper.MapProperty(x => x.Bar, "PROP-BAR");

var t = new Test {Foo = "Value-Foo", Bar = "Value-Bar"};

Console.WriteLine("Single property:");
var propMap = mapper.GetPropertyMap(x => x.Foo);
Console.WriteLine("{0}={1}", propMap, mapper.GetPropertyValue(t, propMap));
Console.WriteLine();

Console.WriteLine("All properties:");
foreach (var item in mapper.GetPropertyValues(t))
    Console.WriteLine("{0}={1}", item.Key, item.Value);

输出:
Single property:
PROP-FOO=Value-Foo

All properties:
PROP-FOO=Value-Foo
PROP-BAR=Value-Bar

文章出处:http://www.codeproject.com/Tips/824912/Mapping-Properties-to-String-Keys

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值