无效的转换异常:无法将类型“__DynamicallyInvokableAttribute”的对象转换为类型“BugFixApplication.DeBugInfo”

Jam*_*mes 0 c# casting runtime-error exception

我尝试将 Attribute 变量类型转换为 DeBugInfo 类型。

这是类 ExecuteRectangle 中 Main 函数的代码:

class ExecuteRectangle
{
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle(4.5, 7.5);
        r.Display();
        Type type = typeof(Rectangle);
        //iterating through the attribtues of the Rectangle class
        foreach (Object attributes in type.GetCustomAttributes(false))
        {
            DeBugInfo dbi = (DeBugInfo)attributes;
            if (null != dbi)
            {
                Console.WriteLine("Bug no: {0}", dbi.BugNo);
                Console.WriteLine("Developer: {0}", dbi.Developer);
                Console.WriteLine("Last Reviewed: {0}",
                     dbi.LastReview);
                Console.WriteLine("Remarks: {0}", dbi.Message);
            }
        }

        //iterating through the method attribtues
        foreach (MethodInfo m in type.GetMethods())
        {
            foreach (Attribute a in m.GetCustomAttributes(true))
            {
                DeBugInfo dbi = (DeBugInfo)a;
                if (null != dbi)
                {
                    Console.WriteLine("Bug no: {0}, for Method: {1}",
                          dbi.BugNo, m.Name);
                    Console.WriteLine("Developer: {0}", dbi.Developer);
                    Console.WriteLine("Last Reviewed: {0}",
                          dbi.LastReview);
                    Console.WriteLine("Remarks: {0}", dbi.Message);
                }
            }
        }
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 DebugInfo 类:

[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]

public class DeBugInfo : Attribute
{
    private int bugNo;
    private string developer;
    private string lastReview;
    public string message;

    public DeBugInfo(int bg, string dev, string d)
    {
        this.bugNo = bg;
        this.developer = dev;
        this.lastReview = d;
    }

    public int BugNo
    {
        get
        {
            return bugNo;
        }
    }
    public string Developer
    {
        get
        {
            return developer;
        }
    }
    public string LastReview
    {
        get
        {
            return lastReview;
        }
    }
    public string Message
    {
        get
        {
            return message;
        }
        set
        {
            message = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

还有矩形类:

  [DeBugInfo(45, "Zara Ali", "12/8/2012",
 Message = "Return type mismatch")]
[DeBugInfo(49, "Nuha Ali", "10/10/2012",
 Message = "Unused variable")]
class Rectangle
{
    //member variables
    protected double length;
    protected double width;
    public Rectangle(double l, double w)
    {
        length = l;
        width = w;
    }
    [DeBugInfo(55, "Zara Ali", "19/10/2012",
     Message = "Return type mismatch")]
    public double GetArea()
    {
        return length * width;
    }
    [DeBugInfo(56, "Zara Ali", "19/10/2012")]
    public void Display()
    {
        Console.WriteLine("Length: {0}", length);
        Console.WriteLine("Width: {0}", width);
        Console.WriteLine("Area: {0}", GetArea());
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我得到所需的输出,如下所示:

Length: 4.5
Width: 7.5
Area: 33.75
Bug No: 49
Developer: Nuha Ali
Last Reviewed: 10/10/2012
Remarks: Unused variable
Bug No: 45
Developer: Zara Ali
Last Reviewed: 12/8/2012
Remarks: Return type mismatch
Bug No: 55, for Method: GetArea
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks: Return type mismatch
Bug No: 56, for Method: Display
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks: 
Run Code Online (Sandbox Code Playgroud)

但我也遇到了这个未处理的异常错误和程序崩溃:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type       
'__DynamicallyInvokableAttribute' to type 'BugFixApplication.DeBugInfo' at 
BugFixApplication.ExecuteRectangle.Main(string[] args)
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会出现此异常,也不知道如何解决此问题。

Ham*_*yan 6

尝试这个

foreach (Object attributes in type.GetCustomAttributes(false))
{
    DeBugInfo dbi = attributes as DeBugInfo;
    if (null != dbi)
    {
        .....
Run Code Online (Sandbox Code Playgroud)

对方法属性使用相同的方法。

  • @AsisDatta 因为 `type.GetCustomAttributes()` 方法返回不同类型的属性。使用此转换语法“(DeBugInfo)attributes”意味着“attributes”变量始终可以明确地转换为“DeBugInfo”。该错误意味着情况并非如此。有些“属性”无法转换为“DeBugInfo”。因此,使用“attributes as DeBugInfo”会导致它尝试进行强制转换,但如果不能则返回 null。这就是为什么答案的下一行检查 null 的原因。`if` 块只会处理可以转换为 `DeBugInfo` 的属性。 (2认同)