DataMember.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / DataMember.cs / 3 / DataMember.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Runtime.Serialization
{ 
    using System;
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Globalization;
    using System.Reflection; 
    using System.Xml;
    using System.Security;

    internal class DataMember 
    {
        ///  
        /// Critical - holds instance of CriticalHelper which keeps state that is cached statically for serialization. 
        ///            Static fields are marked SecurityCritical or readonly to prevent
        ///            data from being modified or leaked to other components in appdomain. 
        /// 
        [SecurityCritical]
        CriticalHelper helper;
 
        /// 
        /// Critical - initializes SecurityCritical field 'helper' 
        /// Safe - doesn't leak anything 
        /// 
        [SecurityCritical, SecurityTreatAsSafe] 
        internal DataMember()
        {
            helper = new CriticalHelper();
        } 

        ///  
        /// Critical - initializes SecurityCritical field 'helper' 
        /// Safe - doesn't leak anything
        ///  
        [SecurityCritical, SecurityTreatAsSafe]
        internal DataMember(MemberInfo memberInfo)
        {
            helper = new CriticalHelper(memberInfo); 
        }
 
        ///  
        /// Critical - initializes SecurityCritical field 'helper'
        /// Safe - doesn't leak anything 
        /// 
        [SecurityCritical, SecurityTreatAsSafe]
        internal DataMember(string name)
        { 
            helper = new CriticalHelper(name);
        } 
 
        /// 
        /// Critical - initializes SecurityCritical field 'helper' 
        /// Safe - doesn't leak anything
        /// 
        [SecurityCritical, SecurityTreatAsSafe]
        internal DataMember(DataContract memberTypeContract, string name, bool isNullable, bool isRequired, bool emitDefaultValue, int order) 
        {
            helper = new CriticalHelper(memberTypeContract, name, isNullable, isRequired, emitDefaultValue, order); 
        } 

        internal MemberInfo MemberInfo 
        {
            [SecurityCritical, SecurityTreatAsSafe]
            get { return helper.MemberInfo; }
        } 

        internal string Name 
        { 
            [SecurityCritical, SecurityTreatAsSafe]
            get { return helper.Name; } 
            [SecurityCritical]
            set  { helper.Name = value; }
        }
 
        internal int Order
        { 
            [SecurityCritical, SecurityTreatAsSafe] 
            get { return helper.Order; }
            [SecurityCritical] 
            set { helper.Order = value; }
        }

        internal bool IsRequired 
        {
            [SecurityCritical, SecurityTreatAsSafe] 
            get { return helper.IsRequired; } 
            [SecurityCritical]
            set { helper.IsRequired = value; } 
        }

        internal bool EmitDefaultValue
        { 
            [SecurityCritical, SecurityTreatAsSafe]
            get { return helper.EmitDefaultValue; } 
            [SecurityCritical] 
            set { helper.EmitDefaultValue = value; }
        } 

        internal bool IsNullable
        {
            [SecurityCritical, SecurityTreatAsSafe] 
            get { return helper.IsNullable; }
            [SecurityCritical] 
            set { helper.IsNullable = value; } 
        }
 
        internal bool IsGetOnlyCollection
        {
            [SecurityCritical, SecurityTreatAsSafe]
            get { return helper.IsGetOnlyCollection; } 
            [SecurityCritical]
            set { helper.IsGetOnlyCollection = value; } 
        } 

        internal Type MemberType 
        {
            [SecurityCritical, SecurityTreatAsSafe]
            get { return helper.MemberType; }
        } 

        internal DataContract MemberTypeContract 
        { 
            [SecurityCritical, SecurityTreatAsSafe]
            get { return helper.MemberTypeContract; } 
            [SecurityCritical]
            set { helper.MemberTypeContract = value; }
        }
 
        internal bool HasConflictingNameAndType
        { 
            [SecurityCritical, SecurityTreatAsSafe] 
            get { return helper.HasConflictingNameAndType; }
            [SecurityCritical] 
            set { helper.HasConflictingNameAndType = value; }
        }

        internal DataMember ConflictingMember 
        {
            [SecurityCritical, SecurityTreatAsSafe] 
            get { return helper.ConflictingMember; } 
            [SecurityCritical]
            set { helper.ConflictingMember = value; } 
        }

        /// 
        /// Critical 
        /// 
        [SecurityCritical(SecurityCriticalScope.Everything)] 
        class CriticalHelper 
        {
            DataContract memberTypeContract; 
            string name;
            int order;
            bool isRequired;
            bool emitDefaultValue; 
            bool isNullable;
            bool isGetOnlyCollection = false; 
            MemberInfo memberInfo; 
            bool hasConflictingNameAndType;
            DataMember conflictingMember; 

            internal CriticalHelper()
            {
                this.emitDefaultValue = Globals.DefaultEmitDefaultValue; 
            }
 
            internal CriticalHelper(MemberInfo memberInfo) 
            {
                this.emitDefaultValue = Globals.DefaultEmitDefaultValue; 
                this.memberInfo = memberInfo;
            }

            internal CriticalHelper(string name) 
            {
                this.Name = name; 
            } 

            internal CriticalHelper(DataContract memberTypeContract, string name, bool isNullable, bool isRequired, bool emitDefaultValue, int order) 
            {
                this.MemberTypeContract = memberTypeContract;
                this.Name = name;
                this.IsNullable = isNullable; 
                this.IsRequired = isRequired;
                this.EmitDefaultValue = emitDefaultValue; 
                this.Order = order; 
            }
 
            internal MemberInfo MemberInfo
            {
                get { return memberInfo; }
            } 

            internal string Name 
            { 
                get { return name; }
                set { name = value; } 
            }

            internal int Order
            { 
                get { return order; }
                set { order = value; } 
            } 

            internal bool IsRequired 
            {
                get { return isRequired; }
                set { isRequired = value; }
            } 

            internal bool EmitDefaultValue 
            { 
                get { return emitDefaultValue; }
                set { emitDefaultValue = value; } 
            }

            internal bool IsNullable
            { 
                get { return isNullable; }
                set { isNullable = value; } 
            } 

            internal bool IsGetOnlyCollection 
            {
                get { return isGetOnlyCollection; }
                set { isGetOnlyCollection = value; }
            } 

            internal Type MemberType 
            { 
                get
                { 
                    FieldInfo field = MemberInfo as FieldInfo;
                    if (field != null)
                        return field.FieldType;
                    return ((PropertyInfo)MemberInfo).PropertyType; 
                }
            } 
 
            internal DataContract MemberTypeContract
            { 
                get
                {
                    if (memberTypeContract == null)
                    { 
                        if (MemberInfo != null)
                        { 
                            if (this.IsGetOnlyCollection) 
                            {
                                memberTypeContract = DataContract.GetGetOnlyCollectionDataContract(DataContract.GetId(MemberType.TypeHandle), MemberType.TypeHandle, MemberType, SerializationMode.SharedContract); 
                            }
                            else
                            {
                                memberTypeContract = DataContract.GetDataContract(MemberType); 
                            }
                        } 
                    } 
                    return memberTypeContract;
                } 
                set
                {
                    memberTypeContract = value;
                } 
            }
 
            internal bool HasConflictingNameAndType 
            {
                get { return this.hasConflictingNameAndType; } 
                set { this.hasConflictingNameAndType = value; }
            }

            internal DataMember ConflictingMember 
            {
                get { return this.conflictingMember; } 
                set { this.conflictingMember = value; } 
            }
        } 

        /// 
        /// Review - checks member visibility to calculate if access to it requires MemberAccessPermission for serialization.
        ///          since this information is used to determine whether to give the generated code access 
        ///          permissions to private members, any changes to the logic should be reviewed.
        ///  
        [SecurityRequiresReview] 
        internal bool RequiresMemberAccessForGet()
        { 
            MemberInfo memberInfo = MemberInfo;
            FieldInfo field = memberInfo as FieldInfo;
            if (field != null)
            { 
                return DataContract.FieldRequiresMemberAccess(field);
            } 
            else 
            {
                MethodInfo getMethod = ((PropertyInfo)memberInfo).GetGetMethod(true /*nonPublic*/); 
                if (getMethod != null)
                    return DataContract.MethodRequiresMemberAccess(getMethod);
            }
            return false; 
        }
 
        ///  
        /// Review - checks member visibility to calculate if access to it requires MemberAccessPermission for deserialization.
        ///          since this information is used to determine whether to give the generated code access 
        ///          permissions to private members, any changes to the logic should be reviewed.
        /// 
        [SecurityRequiresReview]
        internal bool RequiresMemberAccessForSet() 
        {
            MemberInfo memberInfo = MemberInfo; 
            FieldInfo field = memberInfo as FieldInfo; 
            if (field != null)
            { 
                return DataContract.FieldRequiresMemberAccess(field);
            }
            else
            { 
                MethodInfo setMethod = ((PropertyInfo)memberInfo).GetSetMethod(true /*nonPublic*/);
                if (setMethod != null) 
                    return DataContract.MethodRequiresMemberAccess(setMethod); 
            }
            return false; 
        }

        internal DataMember BindGenericParameters(DataContract[] paramContracts, Dictionary boundContracts)
        { 
            DataContract memberTypeContract = this.MemberTypeContract.BindGenericParameters(paramContracts, boundContracts);
            DataMember boundDataMember = new DataMember(memberTypeContract, 
                this.Name, 
                !memberTypeContract.IsValueType,
                this.IsRequired, 
                this.EmitDefaultValue,
                this.Order);
            return boundDataMember;
        } 

        internal bool Equals(object other, Dictionary checkedContracts) 
        { 
            if ((object) this == other)
                return true; 

            DataMember dataMember = other as DataMember;
            if (dataMember != null)
            { 
                // Note: comparison does not use Order hint since it influences element order but does not specify exact order
                bool thisIsNullable = (MemberTypeContract == null) ? false : !MemberTypeContract.IsValueType; 
                bool dataMemberIsNullable = (dataMember.MemberTypeContract == null) ? false : !dataMember.MemberTypeContract.IsValueType; 
                return (Name == dataMember.Name
                        && (IsNullable || thisIsNullable) == (dataMember.IsNullable || dataMemberIsNullable) 
                        && IsRequired == dataMember.IsRequired
                        && EmitDefaultValue == dataMember.EmitDefaultValue
                        && MemberTypeContract.Equals(dataMember.MemberTypeContract, checkedContracts));
            } 
            return false;
        } 
 
        public override int GetHashCode()
        { 
            return base.GetHashCode();
        }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK