值不能为空。参数名:类型。

3

在阅读和尝试StackExchange上的答案后,我仍然无法获取以下代码中结果的空值:

    typeOfIMydServiceBase.Assembly
            .GetExportedTypes()
            .Where(exportedType => typeOfMydServiceBase != exportedType && typeOfMydServiceBase.IsAssignableFrom(exportedType))
            .Select(mydServiceType => new
            {
                serviceType = mydServiceType,
                interfaceType = mydServiceType.GetInterfaces().First(@interface => @interface != typeOfIMydServiceBase && typeOfIMydServiceBase.IsAssignableFrom(@interface))
            })
            .ToList()
            .ForEach(registrationInfo => container.Register(registrationInfo.interfaceType, () =>
            {
                var method = serviceActivatorType
                    .GetMethod("GetService", serviceActivatorParams)
                    .MakeGenericMethod(new[] { registrationInfo.interfaceType });
                var result = method.Invoke(new ServiceActivator(), null);

                return result;
            }, WebApiRequestLifestyleWithDisposal));

结果一直返回为空,并且当我使用 Object[] {null} 或 Object[]{} 或 Object[]{null,null,等等} 时,我得到的是参数不匹配的错误。

注意:这是上面代码的错误信息:

第156行: .MakeGenericMethod(new[] { registrationInfo.interfaceType });

第157行:

第158行: var result = method.Invoke(new ServiceActivator(), null);

第159行:

第160行: return result;

有人知道这里发生了什么以及如何修复吗????

{
public class ServiceActivator
{
    public ServiceActivator();
    public ServiceActivator(IServiceConfigurationReader configurationReader);

    public T GetService<T>();
    public T GetService<T>(string key);
}

与上述内容相关的是所使用服务的注册。请参见下文:

//Scan and register business servics
        var typeOfIMydServiceBase = typeof(IMydServiceBase);
        var typeOfMydServiceBase = typeof(MydServiceBase);
        var serviceActivatorType = typeof(ServiceActivator);
        var serviceActivatorParams = new Type[0];

更新:在serviceActivator Params中使用typeof(string)可以工作,但是我在稍后遇到了一个未处理的异常。这是新的错误:
Line 166:                    p.ServiceContainer = container;
Line 167:                });
Line 168:
Line 169:                container.RegisterWebApiControllers(config);
Line 170:            }

这与以下代码有关:

var typeOfIMydServiceBase = typeof(IMydServiceBase);
        var typeOfMydServiceBase = typeof(MydServiceBase);
        var serviceActivatorType = typeof(ServiceActivator);
        var serviceActivatorParams = new Type[] { typeof(string) };
        
        typeOfIMydServiceBase.Assembly
            .GetExportedTypes()
            .Where(exportedType => typeOfMydServiceBase != exportedType && typeOfMydServiceBase.IsAssignableFrom(exportedType))
            .Select(mydServiceType => new
            {
                serviceType = mydServiceType,
                interfaceType = mydServiceType.GetInterfaces().First(@interface => @interface != typeOfIMydServiceBase && typeOfIMydServiceBase.IsAssignableFrom(@interface))
            })
            .ToList()
            .ForEach(registrationInfo => container.Register(registrationInfo.interfaceType, () =>
            {
                
                var method = serviceActivatorType
                    .GetMethod("GetService", serviceActivatorParams)
                    .MakeGenericMethod(new[] { registrationInfo.interfaceType });
           
                var result = method.Invoke(new ServiceActivator(), null);

                return result;
            }, WebApiRequestLifestyleWithDisposal));
        {

            container.RegisterInitializer<IMydServiceBase>(p =>
            {
                p.ServiceContainer = container;
            });

            container.RegisterWebApiControllers(config);
        }
    }

你的 GetService 方法签名是什么?它的参数允许 null 吗(例如值类型等)? - Khanh TO
@KhanhTO,针对你的问题,GetService是在ServiceActivator函数内定义的。具体代码如下:public class ServiceActivator { public ServiceActivator(); public ServiceActivator(IServiceConfigurationReader configurationReader);public T GetService(); public T GetService(string key);} - Chuckles
1
尝试将.GetMethod("GetService", serviceActivatorParams)替换为.GetMethod("GetService", new Type[] { typeof(string) }) - Khanh TO
@KhanhTO,serviceActivatorParams 目前被定义为以下内容:var serviceActivatorParams = new Type[0]; 当我尝试使用 typeof(string) 时,会出现“无法将 system.Type 转换为 system.Type[]”的错误。 - Chuckles
它应该是 new Type[] { typeof(string) }。如果您使用 serviceActivatorParams,则应为 var serviceActivatorParams = new Type[] { typeof(string) } - Khanh TO
显示剩余7条评论
1个回答

2

var method = serviceActivatorType.GetMethod("GetService", serviceActivatorParams)

返回 public T GetService<T>();,其中var serviceActivatorParams = new Type[0]; (来自问题评论讨论)

但是这行代码:

var result = method.Invoke(new ServiceActivator(), null);

将要调用这个方法:

public T GetService<T>(string key);

传递null作为string key的值。

对于public T GetService<T>();,不需要传递参数,因此它会抛出异常。

为了解决您的问题,请尝试:

var serviceActivatorParams = new Type[] { typeof(string) };

或者在不使用 null 参数的情况下调用您的方法:

var result = method.Invoke(new ServiceActivator());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接