Unity 无法解析动态创建的类的实例

发布于 2024-10-08 03:14:33 字数 12609 浏览 1 评论 0原文

解决方案 我没有在新类型上指定构造函数参数,因此 .NET 假定了基类的类型,但它也没有将它们命名为相同的名称,因此当 Unity 尝试 Parameter[x].Name 时,它​​得到了 Null 值参数名称的。

抱歉使用“U”字,但这确实是一个问题,我需要紧急修复此问题,以解决其他地方的大量内存泄漏(长话短说。)所以如果有人可以帮助解决这个问题我会非常感激的!

我使用 Reflection.Emit 在运行时动态创建类型。动态类型源自运行时指定的基类型,并实现 IController。目的是使用 try...finally 包装对 IController.Execute 的调用并释放对象。

这就是奇怪的地方。我可以创建这种新类型的实例,这样

var requiredDependency = new Logger();
Type interceptingControllerType = CreateInterceptingControllerType(superClass);
var constructorInfo = interceptingControllerType.GetConstructor(new Type[] { typeof(Logger) });
var result = (IController)constructorInfo.Invoke(new object[] { requiredDependency });

我也可以使用 unity 来解析超类,

var superClassThatWillResolve = container.Resolve(superClass);

但我无法使用 unity 来解析动态创建的子类

var newSubClassThatWontResolve = container.Resolve(interceptingControllerType);

当我尝试后者时,我得到以下异常

Microsoft.Practices.Unity.ResolutionFailedException was unhandled
  Message=Resolution of the dependency failed, type = "9d206a0c-2c78-43d1-8907-8227ad1242f1", name = "(none)".
Exception occurred while: while resolving.
Exception is: ArgumentNullException - Value cannot be null.
Parameter name: str
-----------------------------------------------
At the time of the exception, the container was:

  Resolving 9d206a0c-2c78-43d1-8907-8227ad1242f1,(none)

  Source=Microsoft.Practices.Unity
  TypeRequested=9d206a0c-2c78-43d1-8907-8227ad1242f1
  StackTrace:
       at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
       at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides)
       at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)
       at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides)
       at ConsoleApplication18.InterceptingControllerBuilder.CreateControllerInterceptor(IUnityContainer container, Type superClass) in C:\Users\PeterMorris\Documents\Visual Studio 2010\Projects\ConsoleApplication18\InterceptingControllerBuilder.cs:line 24
       at ConsoleApplication18.Program.Main(String[] args) in C:\Users\PeterMorris\Documents\Visual Studio 2010\Projects\ConsoleApplication18\Program.cs:line 15
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ArgumentNullException
       Message=Value cannot be null.
Parameter name: str
       Source=mscorlib
       ParamName=str
       StackTrace:
            at System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, String str)
            at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
            at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
            at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey)
            at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
            at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
            at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
       InnerException: 

这是测试代码...

using System;
using Microsoft.Practices.Unity;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            var childContainer = container.CreateChildContainer();
            IController interceptingController = InterceptingControllerBuilder.CreateControllerInterceptor(
                childContainer, typeof(CountryController));
            try
            {
                interceptingController.Execute("Hello");
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            Console.ReadLine();
        }
    }

    public class Logger
    {
        public void Log(string text)
        {
            Console.WriteLine(text);
        }
    }

    public interface IController
    {
        void Execute(string text);
    }

    public class Controller : IController
    {
        readonly Logger Logger;

        public Controller(Logger logger)
        {
            Logger = logger;
        }

        public virtual void Execute(string text)
        {
            Logger.Log("Controller: " + text);
        }
    }

    public class CountryController : Controller
    {
        public CountryController(Logger logger)
            : base(logger)
        {
        }

        public override void Execute(string text)
        {
            base.Execute("CountryController: " + text);
        }
    }
}

以及实现代码

using Microsoft.Practices.Unity;

namespace ConsoleApplication18
{
    public interface IUnityContainerController
    {
        IUnityContainer IUnityContainerController_UnityContainer { get; set; }
    }
}




using System;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.Practices.Unity;

namespace ConsoleApplication18
{
    public static class InterceptingControllerBuilder
    {
        const string UnityContainerBackingFieldName = "IUnityContainerController_BackingField";
        static MethodInfo DisposeMethodInfo = typeof(IDisposable).GetMethod("Dispose");

        public static IController CreateControllerInterceptor(IUnityContainer container, Type superClass)
        {
            var requiredDependency = new Logger();
            Type interceptingControllerType = CreateInterceptingControllerType(superClass);
            var constructorInfo = interceptingControllerType.GetConstructor(new Type[] { typeof(Logger) });
            var result = (IController)constructorInfo.Invoke(new object[] { requiredDependency });
            var resultAsIUnityContainerController = (IUnityContainerController)result;
            resultAsIUnityContainerController.IUnityContainerController_UnityContainer = container;

            var superClassThatWillResolve = container.Resolve(superClass);
            var newSubClassThatWontResolve = container.Resolve(interceptingControllerType);

            return result;
        }

        static Type CreateInterceptingControllerType(Type superClass)
        {
            if (!typeof(IController).IsAssignableFrom(superClass))
                throw new ArgumentException("SuperClass does not implement IController");

            string guid = Guid.NewGuid().ToString();
            var assemblyName = new AssemblyName(guid);
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                assemblyName,
                AssemblyBuilderAccess.Run);
            var moduleBuilder = assemblyBuilder.DefineDynamicModule(guid);
            var typeBuilder = moduleBuilder.DefineType(
                guid,
                TypeAttributes.Class | TypeAttributes.Public,
                superClass);
            CreateConstructor(superClass, typeBuilder);
            FieldBuilder unityContainerBackingFieldBuilder;
            ImplementIUnityContainerController(typeBuilder, out unityContainerBackingFieldBuilder);
            ImplementIController(superClass, typeBuilder, unityContainerBackingFieldBuilder);
            return typeBuilder.CreateType();
        }

        static void CreateConstructor(Type superClass, TypeBuilder typeBuilder)
        {
            var constructorInfo = superClass.GetConstructors()
                .OrderByDescending(x => x.GetParameters().Count())
                .FirstOrDefault();
            if (constructorInfo == null)
                return;
            ParameterInfo[] constructorParameters =
                constructorInfo.GetParameters().ToArray();
            Type[] parameterTypes = constructorParameters.Select(x => x.ParameterType).ToArray();
            var constructorBuilder = typeBuilder.DefineConstructor(
                MethodAttributes.Public,
                CallingConventions.Standard,
                parameterTypes);
            var bodyGenerator = constructorBuilder.GetILGenerator();
            bodyGenerator.Emit(OpCodes.Ldarg_0);
            for (int argumentIndex = 0; argumentIndex < constructorParameters.Count(); argumentIndex++)
                bodyGenerator.Emit(OpCodes.Ldarg, argumentIndex + 1);
            bodyGenerator.Emit(OpCodes.Call, constructorInfo);
            bodyGenerator.Emit(OpCodes.Ret);
        }

        static void ImplementIUnityContainerController(TypeBuilder typeBuilder, out FieldBuilder unityContainerBackingFieldBuilder)
        {
            typeBuilder.AddInterfaceImplementation(typeof(IUnityContainerController));
            unityContainerBackingFieldBuilder = typeBuilder.DefineField(
                UnityContainerBackingFieldName,
                typeof(IUnityContainer),
                FieldAttributes.Private);

            var propertyAccessorAttributes =
                MethodAttributes.Public | MethodAttributes.SpecialName |
                MethodAttributes.HideBySig | MethodAttributes.Virtual;

            var getterBuilder = typeBuilder.DefineMethod(
                "get_IUnityContainerController_UnityContainer",
                propertyAccessorAttributes,
                typeof(IUnityContainer),
                Type.EmptyTypes);
            var getterGenerator = getterBuilder.GetILGenerator();
            getterGenerator.Emit(OpCodes.Ldarg_0);
            getterGenerator.Emit(OpCodes.Ldfld, unityContainerBackingFieldBuilder);
            getterGenerator.Emit(OpCodes.Ret);

            var setterBuilder = typeBuilder.DefineMethod(
                "set_IUnityContainerController_UnityContainer",
                propertyAccessorAttributes,
                null,
                new Type[] { typeof(IUnityContainer) });
            var setterGenerator = setterBuilder.GetILGenerator();
            setterGenerator.Emit(OpCodes.Ldarg_0);
            setterGenerator.Emit(OpCodes.Ldarg_1);
            setterGenerator.Emit(OpCodes.Stfld, unityContainerBackingFieldBuilder);
            setterGenerator.Emit(OpCodes.Ret);
        }

        static void ImplementIController(Type superClass, TypeBuilder typeBuilder, FieldBuilder unityContainerBackingFieldBuilder)
        {
            typeBuilder.AddInterfaceImplementation(typeof(IController));
            MethodInfo interfaceMethod = typeof(IController).GetMethod("Execute");
            InterfaceMapping mapping = superClass.GetInterfaceMap(typeof(IController));
            MethodInfo baseMethod = mapping.TargetMethods.Single();

            var methodBuilder = typeBuilder.DefineMethod(
                typeof(IController).Name + ".Execute",
                MethodAttributes.Public | MethodAttributes.Virtual | 
                MethodAttributes.ReuseSlot | MethodAttributes.HideBySig,
                null,
                new Type[] { typeof(string) });
            var bodyGenerator = methodBuilder.GetILGenerator();
            bodyGenerator.BeginExceptionBlock();
            bodyGenerator.Emit(OpCodes.Ldarg_0);
            bodyGenerator.Emit(OpCodes.Ldarg_1);
            bodyGenerator.Emit(OpCodes.Call, baseMethod);
            bodyGenerator.BeginFinallyBlock();
            bodyGenerator.Emit(OpCodes.Ldarg_0);
            bodyGenerator.Emit(OpCodes.Ldfld, unityContainerBackingFieldBuilder);
            bodyGenerator.Emit(OpCodes.Call, DisposeMethodInfo);
            bodyGenerator.EndExceptionBlock();
            bodyGenerator.Emit(OpCodes.Ret);
            typeBuilder.DefineMethodOverride(methodBuilder, interfaceMethod);
        }
    }
}

SOLUTION
I did not specify the constructor parameters on the new type so .NET assumed the types of the base class, however it does not also name them the same so when Unity tried Parameter[x].Name it got a Null value instead of a parameter name.

Sorry to use the "U" word, but this really is a problem I need to urgently fix this problem to resolve a massive memory leak elsewhere (long story.) So if anyone can help with this I will really appreciate it!

I am dynamically creating a type at runtime using Reflection.Emit. The dynamic type descends from a base type specified at runtime, and implements IController. The purpose is to wrap the call to IController.Execute with a try...finally and dispose of an object.

Here is what is odd. I can create an instance of this new type like so

var requiredDependency = new Logger();
Type interceptingControllerType = CreateInterceptingControllerType(superClass);
var constructorInfo = interceptingControllerType.GetConstructor(new Type[] { typeof(Logger) });
var result = (IController)constructorInfo.Invoke(new object[] { requiredDependency });

I can also use unity to resolve the superclass like so

var superClassThatWillResolve = container.Resolve(superClass);

But I am unable to use unity to resolve the dynamically created subclass

var newSubClassThatWontResolve = container.Resolve(interceptingControllerType);

When I try the latter I get the following exception

Microsoft.Practices.Unity.ResolutionFailedException was unhandled
  Message=Resolution of the dependency failed, type = "9d206a0c-2c78-43d1-8907-8227ad1242f1", name = "(none)".
Exception occurred while: while resolving.
Exception is: ArgumentNullException - Value cannot be null.
Parameter name: str
-----------------------------------------------
At the time of the exception, the container was:

  Resolving 9d206a0c-2c78-43d1-8907-8227ad1242f1,(none)

  Source=Microsoft.Practices.Unity
  TypeRequested=9d206a0c-2c78-43d1-8907-8227ad1242f1
  StackTrace:
       at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
       at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides)
       at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)
       at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides)
       at ConsoleApplication18.InterceptingControllerBuilder.CreateControllerInterceptor(IUnityContainer container, Type superClass) in C:\Users\PeterMorris\Documents\Visual Studio 2010\Projects\ConsoleApplication18\InterceptingControllerBuilder.cs:line 24
       at ConsoleApplication18.Program.Main(String[] args) in C:\Users\PeterMorris\Documents\Visual Studio 2010\Projects\ConsoleApplication18\Program.cs:line 15
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ArgumentNullException
       Message=Value cannot be null.
Parameter name: str
       Source=mscorlib
       ParamName=str
       StackTrace:
            at System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, String str)
            at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
            at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
            at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey)
            at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
            at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
            at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
       InnerException: 

Here is the test code...

using System;
using Microsoft.Practices.Unity;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            var childContainer = container.CreateChildContainer();
            IController interceptingController = InterceptingControllerBuilder.CreateControllerInterceptor(
                childContainer, typeof(CountryController));
            try
            {
                interceptingController.Execute("Hello");
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            Console.ReadLine();
        }
    }

    public class Logger
    {
        public void Log(string text)
        {
            Console.WriteLine(text);
        }
    }

    public interface IController
    {
        void Execute(string text);
    }

    public class Controller : IController
    {
        readonly Logger Logger;

        public Controller(Logger logger)
        {
            Logger = logger;
        }

        public virtual void Execute(string text)
        {
            Logger.Log("Controller: " + text);
        }
    }

    public class CountryController : Controller
    {
        public CountryController(Logger logger)
            : base(logger)
        {
        }

        public override void Execute(string text)
        {
            base.Execute("CountryController: " + text);
        }
    }
}

And the implementation code

using Microsoft.Practices.Unity;

namespace ConsoleApplication18
{
    public interface IUnityContainerController
    {
        IUnityContainer IUnityContainerController_UnityContainer { get; set; }
    }
}




using System;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.Practices.Unity;

namespace ConsoleApplication18
{
    public static class InterceptingControllerBuilder
    {
        const string UnityContainerBackingFieldName = "IUnityContainerController_BackingField";
        static MethodInfo DisposeMethodInfo = typeof(IDisposable).GetMethod("Dispose");

        public static IController CreateControllerInterceptor(IUnityContainer container, Type superClass)
        {
            var requiredDependency = new Logger();
            Type interceptingControllerType = CreateInterceptingControllerType(superClass);
            var constructorInfo = interceptingControllerType.GetConstructor(new Type[] { typeof(Logger) });
            var result = (IController)constructorInfo.Invoke(new object[] { requiredDependency });
            var resultAsIUnityContainerController = (IUnityContainerController)result;
            resultAsIUnityContainerController.IUnityContainerController_UnityContainer = container;

            var superClassThatWillResolve = container.Resolve(superClass);
            var newSubClassThatWontResolve = container.Resolve(interceptingControllerType);

            return result;
        }

        static Type CreateInterceptingControllerType(Type superClass)
        {
            if (!typeof(IController).IsAssignableFrom(superClass))
                throw new ArgumentException("SuperClass does not implement IController");

            string guid = Guid.NewGuid().ToString();
            var assemblyName = new AssemblyName(guid);
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                assemblyName,
                AssemblyBuilderAccess.Run);
            var moduleBuilder = assemblyBuilder.DefineDynamicModule(guid);
            var typeBuilder = moduleBuilder.DefineType(
                guid,
                TypeAttributes.Class | TypeAttributes.Public,
                superClass);
            CreateConstructor(superClass, typeBuilder);
            FieldBuilder unityContainerBackingFieldBuilder;
            ImplementIUnityContainerController(typeBuilder, out unityContainerBackingFieldBuilder);
            ImplementIController(superClass, typeBuilder, unityContainerBackingFieldBuilder);
            return typeBuilder.CreateType();
        }

        static void CreateConstructor(Type superClass, TypeBuilder typeBuilder)
        {
            var constructorInfo = superClass.GetConstructors()
                .OrderByDescending(x => x.GetParameters().Count())
                .FirstOrDefault();
            if (constructorInfo == null)
                return;
            ParameterInfo[] constructorParameters =
                constructorInfo.GetParameters().ToArray();
            Type[] parameterTypes = constructorParameters.Select(x => x.ParameterType).ToArray();
            var constructorBuilder = typeBuilder.DefineConstructor(
                MethodAttributes.Public,
                CallingConventions.Standard,
                parameterTypes);
            var bodyGenerator = constructorBuilder.GetILGenerator();
            bodyGenerator.Emit(OpCodes.Ldarg_0);
            for (int argumentIndex = 0; argumentIndex < constructorParameters.Count(); argumentIndex++)
                bodyGenerator.Emit(OpCodes.Ldarg, argumentIndex + 1);
            bodyGenerator.Emit(OpCodes.Call, constructorInfo);
            bodyGenerator.Emit(OpCodes.Ret);
        }

        static void ImplementIUnityContainerController(TypeBuilder typeBuilder, out FieldBuilder unityContainerBackingFieldBuilder)
        {
            typeBuilder.AddInterfaceImplementation(typeof(IUnityContainerController));
            unityContainerBackingFieldBuilder = typeBuilder.DefineField(
                UnityContainerBackingFieldName,
                typeof(IUnityContainer),
                FieldAttributes.Private);

            var propertyAccessorAttributes =
                MethodAttributes.Public | MethodAttributes.SpecialName |
                MethodAttributes.HideBySig | MethodAttributes.Virtual;

            var getterBuilder = typeBuilder.DefineMethod(
                "get_IUnityContainerController_UnityContainer",
                propertyAccessorAttributes,
                typeof(IUnityContainer),
                Type.EmptyTypes);
            var getterGenerator = getterBuilder.GetILGenerator();
            getterGenerator.Emit(OpCodes.Ldarg_0);
            getterGenerator.Emit(OpCodes.Ldfld, unityContainerBackingFieldBuilder);
            getterGenerator.Emit(OpCodes.Ret);

            var setterBuilder = typeBuilder.DefineMethod(
                "set_IUnityContainerController_UnityContainer",
                propertyAccessorAttributes,
                null,
                new Type[] { typeof(IUnityContainer) });
            var setterGenerator = setterBuilder.GetILGenerator();
            setterGenerator.Emit(OpCodes.Ldarg_0);
            setterGenerator.Emit(OpCodes.Ldarg_1);
            setterGenerator.Emit(OpCodes.Stfld, unityContainerBackingFieldBuilder);
            setterGenerator.Emit(OpCodes.Ret);
        }

        static void ImplementIController(Type superClass, TypeBuilder typeBuilder, FieldBuilder unityContainerBackingFieldBuilder)
        {
            typeBuilder.AddInterfaceImplementation(typeof(IController));
            MethodInfo interfaceMethod = typeof(IController).GetMethod("Execute");
            InterfaceMapping mapping = superClass.GetInterfaceMap(typeof(IController));
            MethodInfo baseMethod = mapping.TargetMethods.Single();

            var methodBuilder = typeBuilder.DefineMethod(
                typeof(IController).Name + ".Execute",
                MethodAttributes.Public | MethodAttributes.Virtual | 
                MethodAttributes.ReuseSlot | MethodAttributes.HideBySig,
                null,
                new Type[] { typeof(string) });
            var bodyGenerator = methodBuilder.GetILGenerator();
            bodyGenerator.BeginExceptionBlock();
            bodyGenerator.Emit(OpCodes.Ldarg_0);
            bodyGenerator.Emit(OpCodes.Ldarg_1);
            bodyGenerator.Emit(OpCodes.Call, baseMethod);
            bodyGenerator.BeginFinallyBlock();
            bodyGenerator.Emit(OpCodes.Ldarg_0);
            bodyGenerator.Emit(OpCodes.Ldfld, unityContainerBackingFieldBuilder);
            bodyGenerator.Emit(OpCodes.Call, DisposeMethodInfo);
            bodyGenerator.EndExceptionBlock();
            bodyGenerator.Emit(OpCodes.Ret);
            typeBuilder.DefineMethodOverride(methodBuilder, interfaceMethod);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慢慢从新开始 2024-10-15 03:14:33

新类型的构造函数的参数未定义,因此 .NET 创建了一些名称为 NULL 的默认参数!

//Define the parameters for our new constructor
for (int argumentIndex = 0; argumentIndex < parameterTypes.Length; argumentIndex++)
    constructorBuilder.DefineParameter(
        argumentIndex + 1,
        constructorParameters[argumentIndex].Attributes,
        constructorParameters[argumentIndex].Name);

The parameters for the constructor on the new type were not defined so .NET created some default parameters which had NULL for names!

//Define the parameters for our new constructor
for (int argumentIndex = 0; argumentIndex < parameterTypes.Length; argumentIndex++)
    constructorBuilder.DefineParameter(
        argumentIndex + 1,
        constructorParameters[argumentIndex].Attributes,
        constructorParameters[argumentIndex].Name);
初与友歌 2024-10-15 03:14:33

我想说这是因为该类型是动态创建的。与其这样做,为什么不注册一个工厂函数(使用 InjectionFactory) 针对类型?这将阻止 Unity 尝试自行创建类型,而是使用您的自定义函数来创建实例。

顺便问一下,你为什么要动态创建这样的拦截器?您可以在 Unity 中创建拦截器并将其连接起来,而不是动态创建拦截器类型。这将首先消除对所有这些代码的需要。

I'd say it's because the type is created on the fly. Instead of doing it this way, why not register a factory function (using an InjectionFactory) against the type? This will prevent Unity from trying to create the type itself and instead uses your custom function to create the instance.

By the way, why are you creating interceptors like this on the fly? You can create an interceptor in Unity and wire that in rather than dynamically creating interceptor types. This will remove the need for all of this code in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文