分析字段/属性路径



所以我写了这段代码,它可以解析start对象的属性路径,返回所需的属性,并为source对象取一个out参数,返回的属性可以对其调用:

    public static PropertyInfo GetProperty(string path, object start, out object source)
    {
        if (string.IsNullOrEmpty(path))
            throw new ArgumentException();
        source = start;
        var pType = source.GetType();
        var paths = path.Split('.');
        PropertyInfo pInfo = null;
        for (int i = 0; i < paths.Length; i++) {
            var subpath = paths[i];
            pInfo = pType.GetProperty(subpath);
            if (i < paths.Length - 1) { // wonder if there's a better way writing this to avoid this if?
                source = pInfo.GetValue(source);
                pType = source.GetType();
            }
        }
        return pInfo;
    }

现在假设我有以下层次结构:

    public class Object
    {
        public string Name { get; set; }
    }
    public class GameObject : Object { }
    public class Component : Object
    {
        public GameObject gameObject { get; set; }
    }
    public class MonoBehaviour : Component { }
    public class Player : MonoBehaviour { }
    public class GameManager : MonoBehaviour
    {
        public Player player { get; set; }
    }

示例用法:

        var game = new GameManager
        {
            player = new Player { gameObject = new GameObject { Name = "Stu"} }
        };
        // somewhere else...
        object source;
        var name = GetProperty("player.gameObject.Name", game, out source);
        var value = name.GetValue(source); // returns "Stu"

我的问题是:这显然只适用于属性,我如何使它同时适用于属性和字段?-问题是,MemberInfoFieldInfoPropertyInfo之间很常见,但它没有GetValue,所以我不能返回MemberInfo。我一直在读关于表情的文章,但不确定它们在这里对我有什么帮助。。。

同样,我想要的是(给定一个源)解析以下内容的能力:X.Y.Z,其中X、Y、Z可以是属性或字段。

编辑:

所以我修改了一些代码来做我想做的事情——但这不是你所说的干净的代码,太多的参数:

    public static bool TryParse(string path, object start, out PropertyInfo pinfo, out FieldInfo finfo, out object source)
    {
        if (string.IsNullOrEmpty(path))
            throw new ArgumentException();
        var type = start.GetType();
        var paths = path.Split('.');
        source = start;
        pinfo = null;
        finfo = null;
        for (int i = 0; i < paths.Length; i++) {
            var subpath = paths[i];
            pinfo = type.GetProperty(subpath);
            if (pinfo == null) {
                finfo = type.GetField(subpath);
                if (finfo == null)
                    return false;
            }
            if (i < paths.Length - 1) {
                source = pinfo == null ? finfo.GetValue(source) : pinfo.GetValue(source);
                type = source.GetType();
            }
        }
        return true;
    }

用法:

        var game = new GameManager
        {
            player = new Player { gameObject = new GameObject { Name = "Stu" } }
        };
        object source;
        PropertyInfo pinfo;
        FieldInfo finfo;
        if (TryParse("player.gameObject.Name", game, out pinfo, out finfo, out source)) {
            var value = pinfo == null ? finfo.GetValue(source) : pinfo.GetValue(source);
        }

它完成了我想要的解析,但一定有更好的。。。

我认为您可以尝试使用我的免费开源库Dynamic Expresso。

你可以写这样的东西:

var game = new GameManager
    {
        player = new Player { gameObject = new GameObject { Name = "Stu" } }
    };
var interpreter = new Interpreter();
var parameters = new[] {
            new Parameter("game", game)
            };
var result = interpreter.Eval("game.player.gameObject.Name", parameters);

您还可以提取解析后的Expression以获取有关属性/字段的信息,它还支持更复杂的操作(索引器、函数…)。解析后的表达式会被编译,可以调用一次或多次。

这里很热,它可以在表达式树上(不检查null和属性是否存在):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace ExpressionTrees
{
    public static class ExpressionTreeBuilder
    {
        private static readonly IDictionary<string, Delegate> Lambdas = 
            new Dictionary<string, Delegate>();
        public static T GetValue<T, TInst>(this TInst obj, string propPath, T defVal = default(T))
        {
            var key = String.Format("{0};{1}", propPath, "str");//typeof(T).Name);
            Delegate del;
            if (!Lambdas.TryGetValue(key, out del))
            {
                var instance = Expression.Parameter(typeof(TInst), "obj");
                var currentExpression = 
                    propPath
                    .Split('.')
                    .Aggregate((Expression)instance, Expression.PropertyOrField);
                var lexpr = Expression.Lambda<Func<TInst, T>>(currentExpression, instance);
                del = lexpr.Compile();
                Lambdas.Add(key, del);
            }
            var action = (Func<TInst, T>)del;
            return action.Invoke(obj);
        }
    }
}

使用示例:

var surv = new Survey() { id = 1, title = "adsf" , User = new User() { Name = "UserName 11"}};
var dynamicUserName = surv.GetValue<string, Survey>("User.Name");

当然,最好使用经过良好测试和文档记录的第三方库。这个代码片段只是一个例子。

相关内容

  • 没有找到相关文章

最新更新