C# feature request: implement interfaces on anonymous types

前端 未结 10 1541
走了就别回头了
走了就别回头了 2021-02-19 06:57

I am wondering what it would take to make something like this work:

using System;

class Program
{
    static void Main()
    {
        var f = new IFoo { 
              


        
相关标签:
10条回答
  • 2021-02-19 07:13

    I'm going to dump this here. I wrote it a while ago but IIRC it works OK.

    First a helper function to take a MethodInfo and return a Type of a matching Func or Action. You need a branch for each number of parameters, unfortunately, and I apparently stopped at three.

    static Type GenerateFuncOrAction(MethodInfo method)
    {
        var typeParams = method.GetParameters().Select(p => p.ParameterType).ToArray();
        if (method.ReturnType == typeof(void))
        {
            if (typeParams.Length == 0)
            {
                return typeof(Action);
            }
            else if (typeParams.Length == 1)
            {
                return typeof(Action<>).MakeGenericType(typeParams);
            }
            else if (typeParams.Length == 2)
            {
                return typeof(Action<,>).MakeGenericType(typeParams);
            }
            else if (typeParams.Length == 3)
            {
                return typeof(Action<,,>).MakeGenericType(typeParams);
            }
            throw new ArgumentException("Only written up to 3 type parameters");
        }
        else
        {
            if (typeParams.Length == 0)
            {
                return typeof(Func<>).MakeGenericType(typeParams.Concat(new[] { method.ReturnType }).ToArray());
            }
            else if (typeParams.Length == 1)
            {
                return typeof(Func<,>).MakeGenericType(typeParams.Concat(new[] { method.ReturnType }).ToArray());
            }
            else if (typeParams.Length == 2)
            {
                return typeof(Func<,,>).MakeGenericType(typeParams.Concat(new[] { method.ReturnType }).ToArray());
            }
            else if (typeParams.Length == 3)
            {
                return typeof(Func<,,,>).MakeGenericType(typeParams.Concat(new[] { method.ReturnType }).ToArray());
            }
            throw new ArgumentException("Only written up to 3 type parameters");
        }
    }
    

    And now the method that takes an interface as a generic parameter and returns a Type that implements the interface and has a constructor (needs to be called via Activator.CreateInstance) taking a Func or Action for each method/ getter/setter. You need to know the right order to put them in the constructor, though. Alternatively (commented-out code) it can generate a DLL which you can then reference and use the type directly.

    static Type GenerateInterfaceImplementation<TInterface>()
    {
        var interfaceType = typeof(TInterface);
        var funcTypes = interfaceType.GetMethods().Select(GenerateFuncOrAction).ToArray();
    
        AssemblyName aName =
            new AssemblyName("Dynamic" + interfaceType.Name + "WrapperAssembly");
        var assBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                aName,
                AssemblyBuilderAccess.Run/*AndSave*/); // to get a DLL
    
        var modBuilder = assBuilder.DefineDynamicModule(aName.Name/*, aName.Name + ".dll"*/); // to get a DLL
    
        TypeBuilder typeBuilder = modBuilder.DefineType(
            "Dynamic" + interfaceType.Name + "Wrapper",
                TypeAttributes.Public);
    
        // Define a constructor taking the same parameters as this method.
        var ctrBuilder = typeBuilder.DefineConstructor(
            MethodAttributes.Public | MethodAttributes.HideBySig |
                MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
            CallingConventions.Standard,
            funcTypes);
    
    
        // Start building the constructor.
        var ctrGenerator = ctrBuilder.GetILGenerator();
        ctrGenerator.Emit(OpCodes.Ldarg_0);
        ctrGenerator.Emit(
            OpCodes.Call,
            typeof(object).GetConstructor(Type.EmptyTypes));
    
        // For each interface method, we add a field to hold the supplied
        // delegate, code to store it in the constructor, and an
        // implementation that calls the delegate.
        byte methodIndex = 0;
        foreach (var interfaceMethod in interfaceType.GetMethods())
        {
            ctrBuilder.DefineParameter(
                methodIndex + 1,
                ParameterAttributes.None,
                "del_" + interfaceMethod.Name);
    
            var delegateField = typeBuilder.DefineField(
                "del_" + interfaceMethod.Name,
                funcTypes[methodIndex],
                FieldAttributes.Private);
    
            ctrGenerator.Emit(OpCodes.Ldarg_0);
            ctrGenerator.Emit(OpCodes.Ldarg_S, methodIndex + 1);
            ctrGenerator.Emit(OpCodes.Stfld, delegateField);
    
            var metBuilder = typeBuilder.DefineMethod(
                interfaceMethod.Name,
                MethodAttributes.Public | MethodAttributes.Virtual |
                    MethodAttributes.Final | MethodAttributes.HideBySig |
                    MethodAttributes.NewSlot,
                interfaceMethod.ReturnType,
                interfaceMethod.GetParameters()
                    .Select(p => p.ParameterType).ToArray());
    
            var metGenerator = metBuilder.GetILGenerator();
            metGenerator.Emit(OpCodes.Ldarg_0);
            metGenerator.Emit(OpCodes.Ldfld, delegateField);
    
            // Generate code to load each parameter.
            byte paramIndex = 1;
            foreach (var param in interfaceMethod.GetParameters())
            {
                metGenerator.Emit(OpCodes.Ldarg_S, paramIndex);
                paramIndex++;
            }
            metGenerator.EmitCall(
                OpCodes.Callvirt,
                funcTypes[methodIndex].GetMethod("Invoke"),
                null);
    
            metGenerator.Emit(OpCodes.Ret);
            methodIndex++;
        }
    
        ctrGenerator.Emit(OpCodes.Ret);
    
        // Add interface implementation and finish creating.
        typeBuilder.AddInterfaceImplementation(interfaceType);
        var wrapperType = typeBuilder.CreateType();
        //assBuilder.Save(aName.Name + ".dll"); // to get a DLL
    
        return wrapperType;
    }
    

    You can use this as e.g.

    public interface ITest
    {
        void M1();
        string M2(int m2, string n2);
        string prop { get; set; }
    
        event test BoopBooped;
    }
    
    Type it = GenerateInterfaceImplementation<ITest>();
    ITest instance = (ITest)Activator.CreateInstance(it,
        new Action(() => {Console.WriteLine("M1 called"); return;}),
        new Func<int, string, string>((i, s) => "M2 gives " + s + i.ToString()),
        new Func<String>(() => "prop value"),
        new Action<string>(s => {Console.WriteLine("prop set to " + s);}),
        new Action<test>(eh => {Console.WriteLine(eh("handler added"));}),
        new Action<test>(eh => {Console.WriteLine(eh("handler removed"));}));
    
    // or with the generated DLL
    ITest instance = new DynamicITestWrapper(
        // parameters as before but you can see the signature
        );
    
    0 讨论(0)
  • 2021-02-19 07:15

    It requires c# 4, but the opensource framework impromptu interface can fake this out of the box using DLR proxies internally. The performance is good although not as good as if the change you proposed existed.

    using ImpromptuInterface.Dynamic;
    

    ...

    var f = ImpromptuGet.Create<IFoo>(new{ 
                    Foo = "foo",
                    Print = ReturnVoid.Arguments(() => Console.WriteLine(Foo))
                });
    
    0 讨论(0)
  • 2021-02-19 07:17

    I have used in Java the Amonimous Class through the "new IFoo(){...}" sintax and it's practical and easy when you have to quick implement a simple interface.

    As a sample it would be nice to implement IDisposable this way on a legacy object used just one time instead of deriving a new class to implement it.

    0 讨论(0)
  • 2021-02-19 07:19

    There would be a few issues with overloaded members, indexers, and explicit interface implementations.

    However, you could probably define the syntax in a way that allows you to resolve those problems.

    Interestingly, you can get pretty close to what you want with C# 3.0 by writing a library. Basically, you could do this:

    Create<IFoo>
    (
        new
        {
            Foo = "foo",
            Print = (Action)(() => Console.WriteLine(Foo))
        }
    );
    

    Which is pretty close to what you want. The primary differences are a call to "Create" instead of the "new" keyword and the fact that you need to specify a delegate type.

    The declaration of "Create" would look like this:

    T Create<T> (object o)
    {
    //...
    }
    

    It would then use Reflection.Emit to generate an interface implementation dynamically at runtime.

    This syntax, however, does have problems with explicit interface implementations and overloaded members, that you couldn't resolve without changing the compiler.

    An alternative would be to use a collection initializer rather than an anonymous type. That would look like this:

    Create
    {
        new Members<IFoo>
        {
            {"Print", ((IFoo @this)=>Console.WriteLine(Foo))},
            {"Foo", "foo"}
        }
    }
    

    That would enable you to:

    1. Handle explicit interface implementation by specifying something like "IEnumerable.Current" for the string parameter.
    2. Define Members.Add so that you don't need to specify the delegate type in the initializer.

    You would need to do a few things to implement this:

    1. Writer a small parser for C# type names. This only requires ".", "[]", "<>",ID, and the primitive type names, so you could probably do that in a few hours
    2. Implement a cache so that you only generate a single class for each unique interface
    3. Implement the Reflection.Emit code gen. This would probably take about 2 days at the most.
    0 讨论(0)
提交回复
热议问题