Beispiel #1
0
            public CodeMemberMethod GenerateGetHashCodeMethod(Type t)
            {
                var m = new MethodHelper("_GetHashCode", typeof(int),
                                         MemberAttributes.Static | MemberAttributes.Final | MemberAttributes.Public);

                m.Param("value", t);
                m.Add(Decl("hash", typeof(int), Default(typeof(int))));

                if (IsPrimitive(t) || t.IsEnum)
                {
                    m.Assign("hash", Invoke(Var("value"), "GetHashCode"));
                    m.Return("hash");
                    return(m.Code);
                }

                if (HasCustomSerialization(t))
                {
                    m.Add(Stmt("throw new System.Exception(\"Not Implemented\");"));
                    //m.Return(Var("hash"));
                    return(m.Code);
                }

                foreach (FieldInfo field in FieldMetadata(t))
                {
                    CodeExpression fieldHash;
                    if (IsNaiadable(field.FieldType))
                    {
                        AddType(field.FieldType);
                        fieldHash = Invoke(CodeGenClass, "_GetHashCode",
                                           new CodeFieldReferenceExpression(Var("value"), field.Name));
                    }
                    else
                    {
                        var fieldRef = new CodeFieldReferenceExpression(Var("value"), field.Name);
                        fieldHash = Invoke("Naiad.HashCode", "SafeGetHashCode", fieldRef);
                    }

                    m.Assign("hash",
                             new CodeBinaryOperatorExpression(Var("hash"), CodeBinaryOperatorType.Add, fieldHash));
                }

                m.Return(Var("hash"));
                return(m.Code);
            }