重载运算符-StackOverflowException [英] Overloading Operators - StackOverflowException

查看:115
本文介绍了重载运算符-StackOverflowException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Model类,并尝试重写和重载equals和not equals运算符和函数.  实际的Equals函数似乎运行良好,但是当我重载==&的运算符时, !=并在中使用运算符 在相同模型之间的逻辑语句中,我得到程序由于StackOverflowException而终止".  

I am working with Model classes and attempting to override and overload the equals and not equals operators and functions.  The actual Equals function appears to be working fine, but when I overload the operators of == & != and use operators in a logic statement between the same models, I get "The program has been terminated due to a StackOverflowException".  

这是我为每个操作员所做的事情:

Here is what I am doing for each operator:

public static bool operator ==(Model A, Model B) 
{ 
     return IsEqual<Model>(A, B); 
}
public static bool operator !=(Model A, Model B) 
{ 
     return !IsEqual<Model>(A, B); 
}

IsEqual函数位于继承的类和预执行形式中,如下所示:

The IsEqual function is in the inherited class and preforms as below:

        public static bool IsEqaul<T>(T A, T B)
        {
            if ((A == null) && (B == null)) return false;
            try
            {
                PropertyInfo[] PropsA = A.GetType().GetProperties(), PropsB = B.GetType().GetProperties();

                foreach (PropertyInfo pA in PropsA)
                {
                    PropertyInfo pB = PropsA.Where(o => o.Name == pA.Name).FirstOrDefault();

                    switch (pA.GetValue(A, null).GetType().ToString().ToLower().Replace("system.",""))
                    {
                        case "byte": case "sbyte": case "short": case "ushort": case "int16": case "int32": case "int64": case "uint": case "long": case "ulong": case "float": case "double": case "decimal":
                            decimal dec1 = Convert.ToDecimal(pA.GetValue(A, null)), dec2 = Convert.ToDecimal(pB.GetValue(B, null));

                            if (dec1 != dec2) return false;
                            break;
                        case "bool": case "boolean":
                            bool b1 = Convert.ToBoolean(pA.GetValue(A, null)), b2 = Convert.ToBoolean(pB.GetValue(B, null));

                            if (b1 != b2) return false;
                            break;
                        case "char":
                            char c1 = Convert.ToChar(pA.GetValue(A, null)), c2 = Convert.ToChar(pA.GetValue(A, null));

                            if (((char)pA.GetValue(A, null)) != ((char)pB.GetValue(B, null))) return false;
                            break;
                        case "string":
                            string str1 = Convert.ToString(pA.GetValue(A, null)), str2 = Convert.ToString(pB.GetValue(B, null));

                            if (str1 != str2) return false;
                            break;
                        default:
                            string dtStr1 = pA.GetValue(A, null).ToString(), dtStr2 = pA.GetValue(B, null).ToString();

                            if (dtStr1 != dtStr2) return false;
                            break;
                    }
                }

                return true;
            }
            catch { return false; }
        }

我不确定问题出在哪里,因为当我自己运行IsEqual函数时,它可以正常工作.  

I am not sure exactly where things are going wrong because when I run the IsEqual function by its self, then it works fine.  

推荐答案

鲍勃·约翰逊,

Hi Bob F Johnston,

谢谢您在这里发布.

对于您的问题,我举一个简单的示例来测试代码.但是,我在运行时没有得到异常.根据您的代码,将IsEqaul更改为IsEqual.

For your question, I make a simple example to test the code. However, I do not get the exception at run time. According to your code, change IsEqaul to IsEqual.

class Program
    {
        static void Main(string[] args)
        {
            Model a = new Model() { name="Jack"};
            Model b = new Model() { name = "Jack" };
            Model c = new Model() { name = "Tom" };
          
            Console.WriteLine(a==b);
            Console.WriteLine(a ==c);

            Console.ReadKey(); 
        }
    }
    
    public  class Model:Test
    {
       
        public static bool operator ==(Model A, Model B)
        {
            return IsEqual<Model>(A, B);
        }
        public static bool operator !=(Model A, Model B)
        {
            return !IsEqual<Model>(A, B);
        }
        public string name { get; set; }
        
    }

    public class Test
    {
        public static bool IsEqual<T>(T A, T B)
        {
            if ((A == null) && (B == null)) return false;
            try
            {
                PropertyInfo[] PropsA = A.GetType().GetProperties(); //Console.WriteLine(A.GetType());
                PropertyInfo[] PropsB = B.GetType().GetProperties();
                
                foreach (PropertyInfo pA in PropsA)
                {
                    PropertyInfo pB = PropsA.Where(o => o.Name == pA.Name).FirstOrDefault();

                    switch (pA.GetValue(A, null).GetType().ToString().ToLower().Replace("system.", ""))
                    {
                        case "byte":
                        case "sbyte":
                        case "short":
                        case "ushort":
                        case "int16":
                        case "int32":
                        case "int64":
                        case "uint":
                        case "long":
                        case "ulong":
                        case "float":
                        case "double":
                        case "decimal":
                            decimal dec1 = Convert.ToDecimal(pA.GetValue(A, null)), dec2 = Convert.ToDecimal(pB.GetValue(B, null));

                            if (dec1 != dec2) return false;
                            break;
                        case "bool":
                        case "boolean":
                            bool b1 = Convert.ToBoolean(pA.GetValue(A, null)), b2 = Convert.ToBoolean(pB.GetValue(B, null));

                            if (b1 != b2) return false;
                            break;
                        case "char":
                            char c1 = Convert.ToChar(pA.GetValue(A, null)), c2 = Convert.ToChar(pA.GetValue(A, null));

                            if (((char)pA.GetValue(A, null)) != ((char)pB.GetValue(B, null))) return false;
                            break;
                        case "string":
                            string str1 = Convert.ToString(pA.GetValue(A, null)), str2 = Convert.ToString(pB.GetValue(B, null));

                            if (str1 != str2) return false;
                            break;
                        default:
                            string dtStr1 = pA.GetValue(A, null).ToString(), dtStr2 = pA.GetValue(B, null).ToString();

                            if (dtStr1 != dtStr2) return false;
                            break;
                    }
                }

                return true;
            }
            catch { return false; }
        }
    }

您能否提供更多信息?哪里有例外?一些演示代码可能会重复该异常,而更多的错误信息会有所帮助.

Could you provide more information? Where you get the exception? Some demo code which could repeat the exception and the more error information would be helpful.

如果您有任何疑问,请随时告诉我.

Please feel free to to let me know if you have some concerns.

最好的问候,

温迪


这篇关于重载运算符-StackOverflowException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆