本文介绍了堆栈溢出在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个寄存器注册所有我需要的对象:

 公共静态类ObjectRegister
{
    公共静态列表< IObject> RegisteredObjects =新的名单,其中,IObject>();
    静态ObjectRegister()
    {
        RegisteredObjects.Add(新Object1());
        RegisteredObjects.Add(新Object2的());
        RegisteredObjects.Add(新Object3());
    }
}
 

接下来,我有这个功能,检查列表,如果该列表中的项目通过测试,它创建一个对象实例并将其添加到列表中:

 公共静态列表< IObject>扫描(名单<族的参数>名单)
    {
        名单< IObject> neededObjects =新的名单,其中,IObject>();
        的foreach(IObject registeredObject在ObjectRegister.RegisteredObjects)
        {
            的foreach(在列表族的参数参数)
            {
                如果(registeredObject.Test(参数))//返回true或false
                {
                    neededObjects.Add(registeredObject.CreateInstance(参数));
                }
            }
        }
        返回connectedObjects;
    }
 

下面是CreateInstace方法Object1:

 公共IObject的CreateInstance(族的参数参数)
    {
        返回新Object1(参数);
    }
 

和这里的构造函数:

 公共Object1(族的参数newParam)
    {
        this.param = newParam;
    }
 

该公司密切关注该行trowing计算器异常:

  this.param = newParam;
 

尝试了所有的可能性,创建一个实例,默认的构造函数,空对象等等等等,但是毫无效果......任何想法?

感谢名单

编辑:code到Object1类:

 公共类Object1:IObject
{
    公众族的参数参数
    {
        {返回this.param; }
        集合{this.param =价值; }
    }

    内部Object1(){}

    公共Object1(族的参数newParam)
    {
        this.param = newParam;
    }


    公共BOOL测试(族的参数参数)
    {
        //我做参数的propper检查这里,并返回结果
    }


    公共IObject的CreateInstance(族的参数参数)
    {
        返回新Object1(参数);
    }
}
 

解决方案

这是你的问题,在Object1:

 公开族的参数参数{{返回this.param; }集合{this.param =价值; }
 

该属性递归地调用自身 - 这就是为什么你得到一个堆栈溢出。不要那样做。相反,你可能的或者的希望有一个自动实现的属性:

 公开参数参数{获得;组; }
 

或使用专用支持字段:

 私人族的参数参数;
公众族的参数参数{{返回参数; } {设置参数=值; }
 

此外,我强烈建议你开始按照 .NET命名约定和在类型和成员名称要注意拼写。

所以,你可能希望你的类被称为参数 - 虽然我个人倒至少的尝试的,使其多一点点描述, 例如 QueryParameter 或类似的东西。同样 Object1 不完全是一个语义上有意义的名字 - 我希望这不是在名字你的真正的code

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:

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:

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:

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:

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.

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, e.g. QueryParameter or something similar. Likewise Object1 isn't exactly a semantically-meaningful name - I hope it's not the name in your real code.

这篇关于堆栈溢出在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 14:47