C# .dll 中发生类型为“System.StackOverflowException”的未处理异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10024072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:10:27  来源:igfitidea点击:

An unhandled exception of type 'System.StackOverflowException' occurred in .dll

c#windows

提问by user1018213

I'm developing a windows application in C#. In my application I've used one static class.

我正在用 C# 开发 Windows 应用程序。在我的应用程序中,我使用了一个静态类。

Below is the code:

下面是代码:

public static class clsNumber
{
     private static object vValue;

     public static object Value
     {
         get
         {
              return Value;
         }
         set
         {
              Value = value;
         }
     }

     public static string HexValue
     {
         get
         {
              try
              {
                   return Microsoft.VisualBasic.Conversion.Hex(vValue);
              }
              catch
              {
                   return Convert.ToString(vValue);
              }
         }
         set
         {
              Value = Microsoft.VisualBasic.Conversion.Val("&H" + value);
         }
     }
}

From another class the value of "HexValue" of the above mentioned class is being set. Below is the line of code:

从另一个类设置上述类的“HexValue”的值。下面是这行代码:

iStick = sOutPut.Substring(0, 8);
clsNumber.HexValue = iStick;

While executing the above line of code the below mentioned error is coming:

在执行上面的代码行时,出现了下面提到的错误:

"An unhandled exception of type 'System.StackOverflowException' occurred in <.....>.dll"

Can anyone please help me with the resolution to this? Thanks in advance.

任何人都可以帮我解决这个问题吗?提前致谢。

采纳答案by David Heffernan

You have an non-terminating recursion here:

你在这里有一个非终止递归:

public static object Value  
{
     get
     {
          return Value;
     }
     set
     {
           Value = value;      
     }
 }

The line return Valuein the getter calls the getter recursively and that leads to stack overflow. And similarly for the setter.

return Valuegetter 中的行递归调用 getter 并导致堆栈溢出。对于二传手也是如此。

I think you meant:

我想你的意思是:

public static object Value  
{
     get
     {
          return vValue;
     }
     set
     {
           vValue = value;      
     }
 }

i don't see what's to be gained from using an explicitly named field here to back to property. I'd remove vValueand declare the property like this:

我看不出使用此处显式命名的字段返回到属性会获得什么。我会删除vValue并声明这样的属性:

public static object Value { get; set; }

回答by tarakaram gopal

public static string HexValue 

in above line use StringBuilder instead of string

在上面的行中使用 StringBuilder 而不是 string

回答by Dhananjay

2 things :

2件事:

  1. You forgot that you have created vValue field.

  2. Don't Make the Property Recursive (unless really needed, and which is resolvable after some iterations). Here you are calling Value property inside Value property.

  1. 你忘了你已经创建了 vValue 字段。

  2. 不要使属性递归(除非确实需要,并且经过一些迭代后可以解决)。在这里,您在 Value 属性中调用 Value 属性。