gpt4 book ai didi

.net - MethodBuilder.DefineParameter 无法设置参数名称的原因?

转载 作者:行者123 更新时间:2023-12-01 08:20:04 24 4
gpt4 key购买 nike

我正在为 WCF 问题创建一个基于现有接口(interface)的接口(interface),但我有“DefineParameter”没有设置参数名称(创建类型的方法参数没有名称)。
你能看出原因吗?

    public static Type MakeWcfInterface(Type iService)
{
AssemblyName assemblyName = new AssemblyName(String.Format("{0}_DynamicWcf", iService.FullName));
String moduleName = String.Format("{0}.dll", assemblyName.Name);
String ns = iService.Namespace;
if (!String.IsNullOrEmpty(ns)) ns += ".";

// Create assembly
var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);

// Create module
var module = assembly.DefineDynamicModule(moduleName, false);

// Create asynchronous interface type
TypeBuilder iWcfService = module.DefineType(
String.Format("{0}DynamicWcf", iService.FullName),
TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract
);

// Set ServiceContract attributes
iWcfService.SetCustomAttribute(ReflectionEmitHelper.BuildAttribute<ServiceContractAttribute>(null,
new Dictionary<string, object>() {
{ "Name", iService.Name },
}));

iWcfService.SetCustomAttribute(ReflectionEmitHelper.BuildAttribute<ServiceBehaviorAttribute>(null,
new Dictionary<string, object>() {
{ "InstanceContextMode" , InstanceContextMode.Single }
})
);

foreach (var method in iService.GetMethods())
{
BuildWcfMethod(iWcfService, method);
}

return iWcfService.CreateType();
}


private static MethodBuilder BuildWcfMethod(TypeBuilder target, MethodInfo template)
{
// Define method
var method = target.DefineMethod(
template.Name,
MethodAttributes.Abstract | MethodAttributes.Virtual
| MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.VtableLayoutMask | MethodAttributes.HideBySig,
CallingConventions.Standard,
template.ReturnType,
template.GetParameters().Select(p => p.ParameterType).ToArray()
);

// Define parameters
foreach (ParameterInfo param in template.GetParameters())
{
method.DefineParameter(param.Position, ParameterAttributes.None, param.Name);
}

// Set OperationContract attribute
method.SetCustomAttribute(ReflectionEmitHelper.BuildAttribute<OperationContractAttribute>(null, null));

return method;
}

最佳答案

我明白了,所以我告诉你。
答案是我使用 DefineParameter 函数的方式。
GetParameters 函数返回有关所提供方法的参数的信息。
但是DefineParameter函数为所有参数(包括返回参数)设置参数信息,所以位置移位:使用DefineParameter,位置0引用返回参数,调用参数从位置1开始。

查看修复:

method.DefineParameter(param.Position+1, ParameterAttributes.None, param.Name);

STFM(见 fu.... 手册):

MethodBuilder.DefineParameter Method @ MSDN

干杯:)

关于.net - MethodBuilder.DefineParameter 无法设置参数名称的原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2183529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com