how to execute string path on dynamic type?

2019-06-26 09:11发布

问题:

Is it possible to execute string path on dynamic type?

For example having dynamic type we can write

dynamic d = myObj;
var v = d.MyMethod(1,"text").SomeProperty.Name

Now imagine I have string path

string path = "MyMethod(1,\"text\").SomeProperty.Name";
var v = d. //How to applay string path to this type?

回答1:

I have solution using extension method and reflection, you need to optimize and test for different scenario.

EDIT Still dirty code, but supports overloaded method now. I will try to do code clean up and use regex for effective and cleaner solution

You can specify data types for parameters now to the eval method.

string epath = "GetName(System_String: ding dong, System_Int32:1).name";
MyClass cls = new MyClass();
var v = cls.Eval(epath);

Note underscore in type names. This should work without mentioning datatypes if method are not overloaded. Current restriction, you cannot use colon or comma inside string parameter value. :(

Call like var v = d.Execute(path)

        public static object Eval(this object instance, string path)
    {
        string[] cmd = path.Split('.');
        string subString = cmd[0];
        object returnValue = null;
        Type t = instance.GetType();
        if (subString.Contains("("))
        {
            string[] paramString = subString.Split('(');
            string[] parameters = paramString[1].Replace(")", "").Split(new Char[]{','},StringSplitOptions.RemoveEmptyEntries);
            bool hasNoParams = parameters.Length == 0;

            List<Type> typeArray = null;
            if (hasNoParams) typeArray = new List<Type>();
            foreach (string parameter in parameters)
            {
                if (parameter.Contains(":"))
                {
                    if (typeArray == null) typeArray = new List<Type>();
                    string[] typeValue = parameter.Split(':');
                    Type paramType = Type.GetType(typeValue[0].Replace('_','.'));

                    typeArray.Add(paramType);
                }
            }
            MethodInfo info = null;
            if (typeArray == null)
                info = t.GetMethod(paramString[0]);
            else
                info = t.GetMethod(paramString[0], typeArray.ToArray());
            ParameterInfo[] pInfo = info.GetParameters();
            List<object> paramList = new List<object>();
            for (int i = 0; i < pInfo.Length; i++)
            {
                string currentParam = parameters[i];
                if (currentParam.Contains(":"))
                {
                    currentParam = currentParam.Split(':')[1];
                }
                ParameterInfo pram = pInfo[i];
                Type pType = pram.ParameterType;
                object obj = Convert.ChangeType(currentParam, pType);
                paramList.Add(obj);
            }
            if (info == null) returnValue = null;
            else
                returnValue = info.Invoke(instance, paramList.ToArray());
        }
        else
        {

            PropertyInfo pi = t.GetProperty(subString);
            if (pi == null) returnValue = null;
            else
                returnValue = pi.GetValue(instance, null);
        }
        if (returnValue == null || cmd.Length == 1)
            return returnValue;
        else
        {
            returnValue = returnValue.Eval(path.Replace(cmd[0] + ".", ""));
        }
        return returnValue;
    }


回答2:

Not sure if this could be easily achieved. I've been using Spring.NET Expressions in the past.



回答3:

It seems you would need an eval function and C# has none but if this third party C# eval implementation can handle dynamic it might solve your problem.