简体   繁体   English

C#中的堆栈溢出

[英]Stack Overflow in C#

I have this register that registers all the objects I need: 我有这个寄存器来注册我需要的所有对象:

public static class ObjectRegister
{
    public static List<IObject> RegisteredObjects = new List<IObject>();
    static ObjectRegister()
    {
        RegisteredObjects.Add(new Object1());
        RegisteredObjects.Add(new Object2());
        RegisteredObjects.Add(new Object3());
    }
}

Next I have this function that checks a list and if the items in the list pass the test, it creates an object instance and adds it to the list: 接下来,我有一个检查列表的函数,如果列表中的项目通过测试,它将创建一个对象实例并将其添加到列表中:

public static List<IObject> Scan(List<parametar> list)
    {
        List<IObject> neededObjects = new List<IObject>();
        foreach (IObject registeredObject in ObjectRegister.RegisteredObjects)
        {
            foreach (parametar param in list)
            {
                if (registeredObject.Test(param)) //returns true or false 
                {
                    neededObjects.Add(registeredObject.CreateInstance(param));
                }
            }
        }
        return connectedObjects;
    }

Here is the CreateInstace method for Object1: 这是Object1的CreateInstace方法:

public IObject CreateInstance(parametar param)
    {
        return new Object1(param);
    }

And here is the constructor: 这是构造函数:

public Object1(parametar newParam)
    {
        this.param = newParam;
    }

It keeps trowing StackOverflow exception on this line: 它不断在这行上引发StackOverflow异常:

this.param = newParam;

Tried all the possibilities for creating an instance, default constructor, empty object etc etc, but nothing worked... any ideas? 尝试了创建实例,默认构造函数,空对象等的所有可能性,但是没有任何效果……有什么想法吗?

Thanx 谢谢

EDIT: Code to the Object1 class: 编辑:编码到Object1类:

public class Object1: IObject
{
    public parametar param
    {
        get { return this.param; }
        set { this.param = value; }
    }

    internal Object1() { }

    public Object1(parametar newParam)
    {
        this.param = newParam;
    }        


    public bool test(parametar param)
    {
        // I do the propper checking of the param here, and return the result
    }


    public IObject CreateInstance(parametar param)
    {
        return new Object1(param);
    }
} 

This is your problem, in Object1: 这是您在Object1中遇到的问题:

public parametar param { get { return this.param; } set { this.param = value; }

That property calls itself recursively - which is exactly why you're getting a stack overflow. 该属性以递归方式调用自身-这正是为什么会出现堆栈溢出的原因。 Don't do that. 不要那样做 Instead, you probably either want an automatically-implemented property: 相反,你可能要么需要一个自动实现的属性:

public parameter param { get; set; }

or use a private backing field: 或使用私人支持字段:

private parametar param;
public parametar Param { get { return param; } set { param = value; }

Additionally, I'd strongly recommend that you start following .NET naming conventions , and pay attention to spelling in type and member names. 另外,我强烈建议您开始遵循.NET命名约定 ,并注意类型和成员名称的拼写。

So you probably want your class to be called Parameter - although personally I'd at least try to make it a little bit more descriptive, eg QueryParameter or something similar. 因此,您可能希望将您的类称为Parameter尽管就我个人而言,我至少会尝试使其更具描述性,例如QueryParameter或类似名称。 Likewise Object1 isn't exactly a semantically-meaningful name - I hope it's not the name in your real code. 同样, Object1也不是一个语义上有意义的名称-我希望它不是您实际代码中的名称。

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

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