简体   繁体   English

Reflection.TargetInvocationException

[英]Reflection.TargetInvocationException

I have a class named carroms . 我有一个名为carroms的课程 When I create its object, there is no error. 当我创建它的对象时,没有错误。 But when I create an array of carroms then, this exception is thrown: 但是当我创建一个carroms数组时,抛出了这个异常:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll PresentationFramework.dll中出现未处理的“System.Reflection.TargetInvocationException”类型异常

Additional information: Exception has been thrown by the target of an invocation. 附加信息:调用目标引发了异常。

My code for the carroms class: 我的carroms类代码:

class carroms
{

    private bool player;

    public bool checkPlayer
    {
        get { return player; }
        set { player = value; }
    }

    private Point center;

    public Point carromCenter
    {
        get { return center; }
        set { center = value; }
    }

    private Point[] points;

    public Point[] carromPoints
    {
        get { return points; }
        set { points = value; }
    }

    private double width;

    public double carromWidth
    {
        get { return width; }
        set { width = value;
        }
    }

    private double height;

    public double carromHeight
    {
        get { return height; }
        set { height = value; }
    }

    public carroms()
    {
        points = new Point[370];
    }

    public Ellipse draw()
    {
        Ellipse myellipse = new Ellipse();
        myellipse.Height = carromHeight;
        myellipse.Width = carromWidth;
        if (checkPlayer == true)
        {
            myellipse.Fill = Brushes.Black;
        }
        else
        {
            myellipse.Fill = Brushes.Beige;
        }
        return myellipse;
    }
}

And my code for creating the object: 我的代码用于创建对象:

Random randi = new Random();
carroms[] mycarroms = new carroms[5];
mycarroms[0].carromHeight = 100;
mycarroms[0].carromWidth = 100;
mycanvas.Children.Add(mycarroms[0].draw());

Want to add something, Don't get intimidated with TargetInvocationException as it does not serve too much of information. 想要添加一些东西,不要被TargetInvocationException吓倒,因为它不会提供太多信息。 You should See Inner Exception to get the root cause. 您应该看到内部异常以获得根本原因。 InnerException could be of type AggregateException, in that case you need to go further down to get all the exception details. InnerException可以是AggregateException类型,在这种情况下,您需要更进一步以获取所有异常详细信息。

You are creating an array but all items added in array are still null . 您正在创建一个数组,但all items added in array are still null

Initialize them first and then only you can access it. Initialize them first然后只能访问它。 Problem is here - 问题在这里 -

Random randi = new Random();
carroms[] mycarroms = new carroms[5];
mycarroms[0].carromHeight = 100;  <-- mycarroms[0] will be null

It should be - 它应该是 -

Random randi = new Random();
carroms[] mycarroms = new carroms[5];
mycarroms[0] = new carroms();
mycarroms[0].carromHeight = 100;

Or you can use array initializer to initialize it - 或者您可以使用array initializer来初始化它 -

Random randi = new Random();
carroms[] mycarroms = new carroms[5]
   {new carroms(), new carroms(), new carroms(), new carroms(), new carroms()};
mycarroms[0].carromHeight = 100;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM