zoukankan      html  css  js  c++  java
  • C#ValueType实现

    源码

    ValueType是Class,重要的重写了Object.Equals方法,导致值类型行为与引用类型的不同比较行为

    // ==++==
    // 
    //   Copyright (c) Microsoft Corporation.  All rights reserved.
    // 
    // ==--==
    /*============================================================
    **
    ** Class:   ValueType
    **
    **
    ** Purpose: Base class for all value classes.
    **
    **
    ===========================================================*/
    namespace System {
        using System;
        using System.Reflection;
        using System.Runtime.CompilerServices;
        using System.Runtime.Versioning;
     
        [Serializable]
    [System.Runtime.InteropServices.ComVisible(true)]
        public abstract class ValueType {
     
            [System.Security.SecuritySafeCritical]
            public override bool Equals (Object obj) {
                BCLDebug.Perf(false, "ValueType::Equals is not fast.  "+this.GetType().FullName+" should override Equals(Object)");
                if (null==obj) {
                    return false;
                }
                RuntimeType thisType = (RuntimeType)this.GetType();
                RuntimeType thatType = (RuntimeType)obj.GetType();
     
                if (thatType!=thisType) {
                    return false;
                }
     
                Object thisObj = (Object)this;
                Object thisResult, thatResult;
     
                // if there are no GC references in this object we can avoid reflection 
                // and do a fast memcmp
                if (CanCompareBits(this))
                    return FastEqualsCheck(thisObj, obj);
     
                FieldInfo[] thisFields = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
     
                for (int i=0; i<thisFields.Length; i++) {
                    thisResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(thisObj);
                    thatResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(obj);
                    
                    if (thisResult == null) {
                        if (thatResult != null)
                            return false;
                    }
                    else
                    if (!thisResult.Equals(thatResult)) {
                        return false;
                    }
                }
     
                return true;
            }
        }
    }
    
    
  • 相关阅读:
    OGNL和Struts2标签
    Struts2中使用Servlet API步骤
    Struts2配置详解
    Strust的基础情况
    分页套用
    删除(注意,删除后,后面顶上去,所以id会一直变,所以我们用class来定义,因为id是唯一的)
    搭建SpringMVC+Mybatis框架并实现数据库的操作
    使用映射接口实现数据库的操作
    django 路由系统
    http协议
  • 原文地址:https://www.cnblogs.com/Fallever/p/9980900.html
Copyright © 2011-2022 走看看