C# implicit cast “overloading” and reflection problem

前端 未结 2 1594
清歌不尽
清歌不尽 2021-01-21 07:50

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;
u         


        
相关标签:
2条回答
  • 2021-01-21 08:43

    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.

    0 讨论(0)
  • 2021-01-21 08:45

    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().

    0 讨论(0)
提交回复
热议问题