C# 隐式强制转换“重载”和反射问题

发布于 2024-10-08 08:09:03 字数 1567 浏览 4 评论 0原文

我的以下代码有问题(可以编译但崩溃):

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 技术交流群。

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

发布评论

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

评论(2

只是偏爱你 2024-10-15 08:09:03

Convert.ChangeType() 不使用隐式运算符。您需要让 MyBoolean 类型实现 IConvertible

第二个问题是相关的。不使用用户定义的转换运算符。您需要先手动转换它,然后再将其传递给 SetValue()

Convert.ChangeType() does not use implicit operators. You'll need to have your MyBoolean type implement IConvertible.

The second problem is related. User-defined conversion operators are not used. You'd need to convert it manually before passing it to SetValue().

漆黑的白昼 2024-10-15 08:09:03

尝试实施IConvertibleConvert 将您的实例强制转换为该接口以尝试执行转换。

对于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.

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