C# 隐式强制转换“重载”和反射问题
我的以下代码有问题(可以编译但崩溃):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
public struct MyBoolean
{
public bool Value { get; set; }
//cast string -> MyBoolean
public static implicit operator MyBoolean(System.String value)
{
return new MyBoolean() { Value = (value[0] == 'J') };
}
//cast bool -> MyBoolean
public static implicit operator MyBoolean(bool value)
{
return new MyBoolean() { Value = value };
}
//cast MyBoolean -> bool
public static implicit operator bool(MyBoolean value)
{
return value.Value;
}
}
public class Foo
{
public MyBoolean TestProp { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyBoolean myBool = true; //works
myBool = "N"; //works
Foo foo = new Foo();
foo.TestProp = "J"; //works
PropertyInfo pi = foo.GetType().GetProperty("TestProp");
var obj = Convert.ChangeType("J", typeof(MyBoolean)); //throws an InvalidCastException
pi.SetValue(foo, "J", null); //throws an ArgumentException
}
}
}
我已经注释了不起作用的语句。有谁知道为什么 Convert.ChangeType 和 PropertyInfo.SetValue 似乎没有使用 MyBoolean 中定义的“重载”强制转换运算符?
顺便说一句,我在这里浏览了其他几个文档,但没有找到与问题完全匹配的内容。
此致 托马斯
I've got a problem with the following code (which compiles but crashes):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
public struct MyBoolean
{
public bool Value { get; set; }
//cast string -> MyBoolean
public static implicit operator MyBoolean(System.String value)
{
return new MyBoolean() { Value = (value[0] == 'J') };
}
//cast bool -> MyBoolean
public static implicit operator MyBoolean(bool value)
{
return new MyBoolean() { Value = value };
}
//cast MyBoolean -> bool
public static implicit operator bool(MyBoolean value)
{
return value.Value;
}
}
public class Foo
{
public MyBoolean TestProp { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyBoolean myBool = true; //works
myBool = "N"; //works
Foo foo = new Foo();
foo.TestProp = "J"; //works
PropertyInfo pi = foo.GetType().GetProperty("TestProp");
var obj = Convert.ChangeType("J", typeof(MyBoolean)); //throws an InvalidCastException
pi.SetValue(foo, "J", null); //throws an ArgumentException
}
}
}
I've commented the statements that don't work. Does anyone know why Convert.ChangeType and PropertyInfo.SetValue doesn't seem to use the "overloaded" cast operator as defined in MyBoolean?
Btw, I've been browsing through several other docs here but didn't find an exact match of the problem.
Best regards
Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Convert.ChangeType()
不使用隐式运算符。您需要让 MyBoolean 类型实现IConvertible
。第二个问题是相关的。不使用用户定义的转换运算符。您需要先手动转换它,然后再将其传递给
SetValue()
。Convert.ChangeType()
does not use implicit operators. You'll need to have your MyBoolean type implementIConvertible
.The second problem is related. User-defined conversion operators are not used. You'd need to convert it manually before passing it to
SetValue()
.尝试实施IConvertible。
Convert
将您的实例强制转换为该接口以尝试执行转换。对于PropertyInfo.SetValue,它获取属性的
Set
方法。当通过反射(AFAICT)调用此方法时,将按类型检查参数,而不是隐式转换为正确类型的能力。必须在调用之前执行此强制转换。Try implementing IConvertible.
Convert
casts your instance to that interface in an attempt to perform conversion.As for PropertyInfo.SetValue, it gets the
Set
method of the property. When this method is invoked via reflection, AFAICT, the arguments are checked by type rather than the ability to implicitly be cast to the proper type. This cast must be performed before invoking.