Как я могу сгенерировать реализацию этого свойства, используя выражения вместо генерации IL?

Я пытаюсь генерировать классы во время выполнения, которые реализуют методы получения свойств с телом, которое вызывает метод базового класса сгенерированного класса. Вот пример простого интерфейса, а также рукописная реализация, которую я пытаюсь продублировать, и базовый класс.

public interface IGenerated : IBase { decimal Property1 { get; } }

public class GeneratedByHand : ImplBase<IGenerated> { 
    public decimal Property1 { get { return Get(s => s.Property1); } }
}

public interface IBase { string _KeyPrefix { get; set; } }

public abstract class ImplBase<T> : IBase
    where T : IBase
{
    public virtual string _KeyPrefix { get; set; }

    protected virtual TResult Get<TResult>(Expression<Func<T, TResult>> property) { 
        return GetValue<TResult>(GetPropertyName(property)); 
    }

    private string GetPropertyName<TResult>(Expression<Func<T, TResult>> property) { 
        return ""; // reflection stuff to get name from property expression goes here
    }
    private TResult GetValue<TResult>(string keyPart) { 
        return default(TResult); // does something like: return ReallyGetValue<TResult>(_KeyPrefix + keyPart);
    }
}

У меня есть рабочая реализация генератора, который использует IL для создания метода, но если я смогу сделать это с помощью выражений, я думаю, что его будет легче расширять и поддерживать. Мне нужно будет искать пользовательские атрибуты в определениях свойств и использовать их для вызова различных перегрузок методов базового класса в реализациях свойств.

Вот где я получил построение выражения для реализации свойства get. Чего я не понимаю, так это построения выражения Call, если я правильно его настраиваю, чтобы сделать эквивалент this.Get() или же base.Get(), Прямо сейчас он бросает System.ArgumentException : Invalid argument value Parameter name: method в CompileToMethod

public void CreateExpressionForGetMethod(MethodBuilder getBuilder, Type interfaceType, Type baseType, PropertyInfo property, MethodInfo getMethod)
{
    var settingsParam = Expression.Parameter(interfaceType, "s");
    var propGetterExpr = Expression.Property(settingsParam, property);

    var propGetterExprFuncType = typeof(Func<,>).MakeGenericType(interfaceType, property.PropertyType);
    var propGetterLambda = Expression.Lambda(propGetterExprFuncType, propGetterExpr, settingsParam);

    var baseGetMethodInfo = 
        baseType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
        .Where(m => {
            var parameters = m.GetParameters();
            return m.Name == "Get" &&
                    parameters != null && parameters.Count() == 1 && parameters[0].ParameterType != typeof(string);
        })
        .First().MakeGenericMethod(property.PropertyType);

    var getExprType = typeof(Expression<>).MakeGenericType(propGetterExprFuncType);
    var getExprParam = Expression.Parameter(getExprType, "expression");

    var getCallExpr = Expression.Call(Expression.Parameter(baseType, "inst"), baseGetMethodInfo, propGetterLambda);

    var getFuncType = typeof(Func<,>).MakeGenericType(getExprType, property.PropertyType);
    var propLambda = Expression.Lambda(getFuncType, getCallExpr, getExprParam);
    propLambda.CompileToMethod(getBuilder);
}

Я не совсем уверен, куда идти отсюда. Я пробовал несколько других вариантов аргументов Expression.Call, но все остальное имело исключения Calling для параметров неправильных типов.

Вот сборная версия всего примера кода, с которым я работаю, включая работающий эмиттер IL:

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using NUnit.Framework;



namespace ExpressionGenerationTest
{
    [TestFixture]
    public class UnitTests
    {
        [Test]
        public void CreateAndSaveAssembly()
        {
            var implGenerator = new ImplBuilder();
            var generatedType = implGenerator.CreateImplementation(typeof(IGenerated));
            implGenerator.SaveAssembly();
        }
    }


    public interface IBase { string _KeyPrefix { get; set; } }


    public abstract class ImplBase<T> : IBase
        where T : IBase
    {
        public virtual string _KeyPrefix { get; set; }

        protected virtual TResult Get<TResult>(Expression<Func<T, TResult>> property) { return GetValue<TResult>(GetPropertyName(property)); }

        private string GetPropertyName<TResult>(Expression<Func<T, TResult>> property) { return ""; } // reflection stuff to get name from property expression goes here
        private TResult GetValue<TResult>(string keyPart) { return default(TResult); } // does something like: return ReallyGetValue(_KeyPrefix + keyPart);
    }


    public interface IGenerated : IBase { decimal Property1 { get; } }

    public class GeneratedByHand : ImplBase<IGenerated> { public decimal Property1 { get { return Get(s => s.Property1); } } }



    public class ImplBuilder
    {
        private const string _assemblyNameBase = "ExpressionGenerationTest.Impl";

        public static ImplBuilder Default { get { return _default.Value; } }
        private static readonly Lazy<ImplBuilder> _default = new Lazy<ImplBuilder>(() => new ImplBuilder());

        private ConcurrentDictionary<Type, Type> _types = new ConcurrentDictionary<Type, Type>();
        private AssemblyBuilder _assemblyBuilder = null;
        private volatile ModuleBuilder _moduleBuilder = null;
        private object _lock = new object();

        private void EnsureInitialized()
        {
            if (_moduleBuilder == null) {
                lock (_lock) {
                    if (_moduleBuilder == null) {
                        _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(_assemblyNameBase), AssemblyBuilderAccess.RunAndSave);
                        _moduleBuilder = _assemblyBuilder.DefineDynamicModule(_assemblyBuilder.GetName().Name, _assemblyNameBase + ".dll");
                    }
                }
            }
        }

        public void SaveAssembly() { _assemblyBuilder.Save(_assemblyNameBase + ".dll"); }
        public TSettings CreateInstance<TSettings>() { return (TSettings)Activator.CreateInstance(_types.GetOrAdd(typeof(TSettings), CreateImplementation)); }
        public void CreateImplementations(IEnumerable<Type> types) { foreach (var t in types) _types.GetOrAdd(t, InternalCreateImplementation); }
        public Type CreateImplementation(Type interfaceType) { return _types.GetOrAdd(interfaceType, InternalCreateImplementation); }
        private Type InternalCreateImplementation(Type interfaceType)
        {
            EnsureInitialized();

            var baseType = typeof (ImplBase<>).MakeGenericType(interfaceType);
            var typeBuilder = _moduleBuilder.DefineType(
                (interfaceType.IsInterface && interfaceType.Name.StartsWith("I") 
                    ? interfaceType.Name.Substring(1) 
                    : interfaceType.Name) + "Impl",
                TypeAttributes.Public | TypeAttributes.Class |
                TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
                TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout,
                baseType,
                new [] {interfaceType});

            foreach (var p in GetPublicProperties(interfaceType).Where(pi => pi.DeclaringType != typeof(IBase))) {
                var iGet = p.GetGetMethod();
                if (iGet != null) {
                    var getBuilder =
                        typeBuilder.DefineMethod(iGet.Name,
                            MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
                            p.PropertyType, Type.EmptyTypes);

                    //EmitILForGetMethod(getBuilder, interfaceType, baseType, p, iGet);
                    CreateExpressionForGetMethod(getBuilder, interfaceType, baseType, p, iGet);
                    typeBuilder.DefineMethodOverride(getBuilder, iGet);
                }
            }

            var implementationType = typeBuilder.CreateType();
            return implementationType;
        }


        public void CreateExpressionForGetMethod(MethodBuilder getBuilder, Type interfaceType, Type baseType, PropertyInfo property, MethodInfo getMethod)
        {
            var settingsParam = Expression.Parameter(interfaceType, "s");
            var propGetterExpr = Expression.Property(settingsParam, property);

            var propGetterExprFuncType = typeof(Func<,>).MakeGenericType(interfaceType, property.PropertyType);
            var propGetterLambda = Expression.Lambda(propGetterExprFuncType, propGetterExpr, settingsParam);

            var baseGetMethodInfo = 
                baseType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
                .Where(m => {
                    var parameters = m.GetParameters();
                    return m.Name == "Get" &&
                            parameters != null && parameters.Count() == 1 && parameters[0].ParameterType != typeof(string);
                })
                .First().MakeGenericMethod(property.PropertyType);

            var getExprType = typeof(Expression<>).MakeGenericType(propGetterExprFuncType);
            var getExprParam = Expression.Parameter(getExprType, "expression");

            var getCallExpr = Expression.Call(Expression.Parameter(baseType, "inst"), baseGetMethodInfo, propGetterLambda);

            var getFuncType = typeof(Func<,>).MakeGenericType(getExprType, property.PropertyType);
            var propLambda = Expression.Lambda(getFuncType, getCallExpr, getExprParam);
            propLambda.CompileToMethod(getBuilder);
        }


        public void EmitILForGetMethod(MethodBuilder getBuilder, Type interfaceType, Type baseType, PropertyInfo property, MethodInfo getMethod)
        {
            var getGen = getBuilder.GetILGenerator();
            var retVal = getGen.DeclareLocal(property.PropertyType);
            var expParam = getGen.DeclareLocal(typeof(ParameterExpression));
            var expParams = getGen.DeclareLocal(typeof(ParameterExpression[]));
            getGen.Emit(OpCodes.Ldarg_0);
            getGen.Emit(OpCodes.Ldtoken, interfaceType);
            getGen.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle"));
            getGen.Emit(OpCodes.Ldstr, "s");
            getGen.Emit(OpCodes.Call, typeof(Expression).GetMethod("Parameter", new [] {typeof(Type), typeof(string)}));
            getGen.Emit(OpCodes.Stloc, expParam);
            getGen.Emit(OpCodes.Ldloc, expParam);
            getGen.Emit(OpCodes.Ldtoken, getMethod);
            getGen.Emit(OpCodes.Call, typeof(MethodBase).GetMethod("GetMethodFromHandle", new [] {typeof(RuntimeMethodHandle)}, null));
            getGen.Emit(OpCodes.Castclass, typeof(MethodInfo));
            getGen.Emit(OpCodes.Call, typeof(Expression).GetMethod("Property", new[] {typeof(Expression), typeof(MethodInfo)}));
            getGen.Emit(OpCodes.Ldc_I4_1);
            getGen.Emit(OpCodes.Newarr, typeof(ParameterExpression));
            getGen.Emit(OpCodes.Stloc, expParams);
            getGen.Emit(OpCodes.Ldloc, expParams);
            getGen.Emit(OpCodes.Ldc_I4_0);
            getGen.Emit(OpCodes.Ldloc, expParam);
            getGen.Emit(OpCodes.Stelem_Ref);
            getGen.Emit(OpCodes.Ldloc, expParams);

            var lambdaMethodInfo = 
                typeof(Expression).GetMethods(BindingFlags.Public | BindingFlags.Static)
                .Where(x => { 
                    var parameters = x.GetParameters();
                    return x.Name == "Lambda" && 
                            x.IsGenericMethodDefinition &&
                            parameters.Count() == 2 &&
                            parameters[0].ParameterType == typeof(Expression) &&
                            parameters[1].ParameterType == typeof(ParameterExpression[]);
                }).FirstOrDefault();

            var lambdaFuncType = typeof(Func<,>);
            lambdaFuncType = lambdaFuncType.MakeGenericType(interfaceType, property.PropertyType);
            lambdaMethodInfo = lambdaMethodInfo.MakeGenericMethod(lambdaFuncType);

            getGen.Emit(OpCodes.Call, lambdaMethodInfo);


            var baseGetMethodInfo = 
                baseType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
                .Where(m => {
                    var parameters = m.GetParameters();
                    return m.Name == "Get" &&
                            parameters != null && parameters.Count() == 1 && parameters[0].ParameterType != typeof(string);
                }).FirstOrDefault();

            baseGetMethodInfo = baseGetMethodInfo.MakeGenericMethod(property.PropertyType);

            getGen.Emit(OpCodes.Callvirt, baseGetMethodInfo);

            getGen.Emit(OpCodes.Stloc_0);
            var endOfMethod = getGen.DefineLabel();
            getGen.Emit(OpCodes.Br_S, endOfMethod);
            getGen.MarkLabel(endOfMethod);
            getGen.Emit(OpCodes.Ldloc_0);
            getGen.Emit(OpCodes.Ret);
        }


        // from http://stackru.com/a/2444090/224087
        public static PropertyInfo[] GetPublicProperties(Type type)
        {
            if (!type.IsInterface)
                return type.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance);

            var propertyInfos = new List<PropertyInfo>();
            var considered = new List<Type>();
            var queue = new Queue<Type>();

            considered.Add(type);
            queue.Enqueue(type);
            while (queue.Count > 0) {
                var subType = queue.Dequeue();

                foreach (var subInterface in subType.GetInterfaces()) {
                    if (considered.Contains(subInterface)) 
                        continue;

                    considered.Add(subInterface);
                    queue.Enqueue(subInterface);
                }

                var typeProperties = subType.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance);
                var newPropertyInfos = typeProperties.Where(x => !propertyInfos.Contains(x));
                propertyInfos.InsertRange(0, newPropertyInfos);
            }

            return propertyInfos.ToArray();
        }
    }
}

1 ответ

Что я не очень понимаю, так это построение выражения Call, если я правильно его настраиваю, чтобы сделать эквивалент this.Get() или base.Get().

Если вы вызываете виртуальный метод, то this.Get(), который обращается к наиболее производному переопределению (которое может быть даже определено в потомке текущего класса), использует callvirt инструкция И не имеет значения, какой тип вы отражаете, чтобы получить MethodInfoпотому что они все используют один и тот же виртуальный слот таблицы.

Излучать base.Get(), Вы должны

  • использовать call инструкция
  • отражать против типа базового класса

Так как callvirt делает некоторые дополнительные вещи, помимо поиска в v-таблице, включая проверку нулевого указателя, компилятор C# использует его для всех виртуальных и не виртуальных вызовов, кроме тех, которые связаны с base ключевое слово.

В частности, анонимные делегаты и лямбды не могут использовать base ключевое слово, поскольку только типы-потомки могут совершать не виртуальные вызовы виртуальных методов (по крайней мере, в проверяемом коде), а лямбда-код фактически размещается в типе замыкания.

Так что, к сожалению, для вашего варианта использования нет способа выразить базовый вызов, используя лямбда-нотацию или деревья выражений. Expression.CompileToMethod только генерирует callvirt, Ну, это не совсем правильно. Генерирует call для вызовов статических методов и методов экземпляров типов значений. Но методы экземпляра ссылочных типов используют только callvirt, Вы можете увидеть это в System.Linq.Expressions.Compiler.LambdaCompiler.UseVirtual

Спасибо @hvd за подтверждение этого на основе комментариев, найденных в справочном источнике Microsoft для UseVirtual

Другие вопросы по тегам