Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用CustomAttributeBuilder时参数不匹配(装箱小数?)_C#_.net_Reflection_Reflection.emit - Fatal编程技术网

C# 使用CustomAttributeBuilder时参数不匹配(装箱小数?)

C# 使用CustomAttributeBuilder时参数不匹配(装箱小数?),c#,.net,reflection,reflection.emit,C#,.net,Reflection,Reflection.emit,我有以下代码作为使用reflection.emit生成接口的系统的一部分 void IPropertyCreator.AddAttributeparams对象[]参数 { //将参数转换为类型 var argTypes=新类型[args.Length]; 对于int i=0;i

我有以下代码作为使用reflection.emit生成接口的系统的一部分

void IPropertyCreator.AddAttributeparams对象[]参数 { //将参数转换为类型 var argTypes=新类型[args.Length]; 对于int i=0;i 公共默认值属性对象值 此代码适用于所有POD类型byte、char、int等,也适用于字符串,但在使用decimal时失败。CustomAttributeBuilder的构造函数失败,索引0处传入的参数值与参数类型不匹配

调试表明,所有变量均符合预期: args有一个object{decimal}类型的元素 argTypes有一个元素类型=System.Decimal ctorInfo已正确选择唯一采用对象参数的构造函数

我已经演示了可以通过直接传递十进制参数来实例化该属性:

十进制值=123.456M; var attr=新的DefaultValueAttributeval; 我已经尝试将十进制转换为对象和系统。十进制无效。 我怀疑这个问题与decimal本身不是一个POD类型而是一个结构这一事实有关

我尝试向采用decimal类型的属性添加构造函数重载。上述函数正确地拾取了新构造函数,但随后在同一位置失败,例外情况是使用了无效类型作为自定义属性构造函数参数、字段或属性


有人知道我如何解决这个问题吗?

试着用普通C代码编写:

class TestAttribute : Attribute {
    public TestAttribute(object value) { }
}

[Test(1.2m)]         // NOTE: CS0182
class Example { }

您不能将System.Decimal用于属性构造函数参数。双人也可以。C语言规范的第17.1.3节暗示了这个问题,但对于这个特殊情况并没有太具体。十进制有点像标准值类型的继代。例如,Ecma 335中从未提及。C编译器使它看起来像一个基本类型,但CLR不以同样的方式处理它。

试着用普通C代码编写:

class TestAttribute : Attribute {
    public TestAttribute(object value) { }
}

[Test(1.2m)]         // NOTE: CS0182
class Example { }
您不能将System.Decimal用于属性构造函数参数。双人也可以。C语言规范的第17.1.3节暗示了这个问题,但对于这个特殊情况并没有太具体。十进制有点像标准值类型的继代。例如,Ecma 335中从未提及。C编译器使它看起来像一个基本类型,但CLR不以同样的方式处理它