// ==++== // // // Copyright (c) 2002 Microsoft Corporation. All rights reserved. // // The use and distribution terms for this software are contained in the file // named license.txt, which can be found in the root of this distribution. // By using this software in any fashion, you are agreeing to be bound by the // terms of this license. // // You must not remove this notice, or any other, from this software. // // // ==--== namespace System { using System; using System.Reflection; using System.Threading; using System.Runtime.Serialization; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate"]/*' /> [Serializable()] publicabstractclass Delegate : ICloneable, ISerializable { // _method is the MethodInfo representing the target, this is set by // InternalCreate. private IntPtr _methodPtr; // _target is the object we will invoke on private Object _target; private RuntimeMethodInfo _method =null; // In the case of a static method passed to a delegate, this field stores // whatever _methodPtr would have stored: and _methodPtr points to a // small thunk which removes the "this" pointer before going on // to _methodPtrAux. private IntPtr _methodPtrAux = IntPtr.Zero; // This constructor is called from the class generated by the // compiler generated code /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.Delegate"]/*' /> protected Delegate(Object target,String method) { if (target ==null) thrownew ArgumentNullException("target"); if (method ==null) thrownew ArgumentNullException("method"); InternalCreate(target,method,false); } // This constructor is called from a class to generate a // delegate based upon a static method name and the Type object // for the class defining the method. /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.Delegate1"]/*' /> protected Delegate(Type target,String method) { if (target ==null) thrownew ArgumentNullException("target"); if (!(target is RuntimeType)) thrownew ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "target"); if (method ==null) thrownew ArgumentNullException("method"); InternalCreateStatic((RuntimeType)target,method); } // Protect the default constructor so you can't build a delegate private Delegate() {} /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.DynamicInvoke"]/*' /> public Object DynamicInvoke(Object[] args) { return DynamicInvokeImpl(args); } /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.DynamicInvokeImpl"]/*' /> protectedvirtual Object DynamicInvokeImpl(Object[] args) { if (_method ==null) _method = InternalFindMethodInfo(); // Use internal version of invoke to avoid access check (performed // during delegate creation). return _method.InternalInvoke(_target,BindingFlags.Default,null,args,null,false); } // equals returns true IIF the delegate is not null and has the // same target, method and invocation list as this object /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.Equals"]/*' /> publicoverridebool Equals(Object obj) { if (obj !=null&& obj is Delegate) { Delegate d = (Delegate) obj; if (IsStatic()) { if (_methodPtrAux==d._methodPtrAux) { returntrue; } }else{ if (d._target == _target && Method.Equals(d.Method)) { returntrue; } } } returnfalse; } /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.GetHashCode"]/*' /> publicoverrideint GetHashCode() { if (IsStatic()) returnunchecked((int)((long)this._methodPtrAux)); else returnunchecked((int)((long)this._methodPtr)); } // Combine creates a new delegate based upon the contents of the // delegates passed in. /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.Combine"]/*' /> publicstatic Delegate Combine(Delegate a, Delegate b) { // boundry conditions -- if either (or both) delegates is null // return the other. if (a ==null) return b; if (b ==null) return a; // Verify that the types are the same if (a.GetType() != b.GetType()) thrownew ArgumentException(Environment.GetResourceString("Arg_DlgtTypeMis")); return a.CombineImpl(b); } // This method creates a new delegate based upon the passed // array of delegates. /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.Combine1"]/*' /> publicstatic Delegate Combine(Delegate[] delegates) { if (delegates ==null|| delegates.Length ==0) returnnull; Delegate d = delegates[0]; for (int i =1; i < delegates.Length; i++) d = Combine(d,delegates[i]); return d; } // Return an array of delegates that represent the invocation list. // This is basically THIS for a Delegate. MulticastDelegates may // have multiple members. /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.GetInvocationList"]/*' /> publicvirtual Delegate[] GetInvocationList() { Delegate[] d =new Delegate[1]; d[0] =this; return d; } // This routine will return the method /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.Method"]/*' /> public MethodInfo Method { get{ if (_method ==null) _method = InternalFindMethodInfo(); return _method; } } /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.GetMethodImpl"]/*' /> protectedvirtual MethodInfo GetMethodImpl() { return Method; } // This routine will return the target /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.Target"]/*' /> public Object Target { get{return IsStatic() ?null : _target;} } //A quick test to see if this is a delegate to a static method. privatebool IsStatic() { if (_target is Delegate) { returntrue; } returnfalse; } // This will remove the value delegate from the source delegate // if it found. /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.Remove"]/*' /> publicstatic Delegate Remove(Delegate source, Delegate value) { if (source ==null) returnnull; if (value ==null) return source; return source.RemoveImpl(value); } // This is an internal routine that is called to do the combine. We // use this to do the combine because the Combine routines are static // final methods. In Delegate, this simply throws a MulticastNotSupportedException // error. Multicast delegate must implement this. /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.CombineImpl"]/*' /> protectedvirtual Delegate CombineImpl(Delegate d) { thrownew MulticastNotSupportedException(Environment.GetResourceString("Multicast_Combine")); } // This is an internal routine that is called to do the remove. We use this // to do the remove because Remove is a static final method. Here we simply // make sure that d is equal to this and return null or this. /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.RemoveImpl"]/*' /> protectedvirtual Delegate RemoveImpl(Delegate d) { if (_target == d._target && Method.Equals(d.Method)) //if (_target == d._target && _methodPtr == d._methodPtr) returnnull; else returnthis; } /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.Clone"]/*' /> publicvirtual Object Clone() { return MemberwiseClone(); } // Create a new delegate given a delegate class, an object and the // name of a method. /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.CreateDelegate"]/*' /> publicstatic Delegate CreateDelegate(Type type,Object target,String method) { if (type ==null) thrownew ArgumentNullException("type"); if (!(type is RuntimeType)) thrownew ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type"); if (target ==null) thrownew ArgumentNullException("target"); if (method ==null) thrownew ArgumentNullException("method"); Type c = type.BaseType; if (c ==null|| (c !=typeof(Delegate) && c !=typeof(MulticastDelegate))) thrownew ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); Delegate d = InternalAlloc((RuntimeType)type); d.InternalCreate(target,method,false); return d; } // Create a new delegate given a delegate class, an object and the // name of a method. /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.CreateDelegate3"]/*' /> publicstatic Delegate CreateDelegate(Type type,Object target,String method,bool ignoreCase) { if (type ==null) thrownew ArgumentNullException("type"); if (!(type is RuntimeType)) thrownew ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type"); if (target ==null) thrownew ArgumentNullException("target"); if (method ==null) thrownew ArgumentNullException("method"); Type c = type.BaseType; if (c ==null|| (c !=typeof(Delegate) && c !=typeof(MulticastDelegate))) thrownew ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); Delegate d = InternalAlloc((RuntimeType)type); d.InternalCreate(target,method,ignoreCase); return d; } /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.CreateDelegate1"]/*' /> publicstatic Delegate CreateDelegate(Type type, Type target, String method) { if (type ==null) thrownew ArgumentNullException("type"); if (!(type is RuntimeType)) thrownew ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type"); if (target ==null) thrownew ArgumentNullException("target"); if (!(target is RuntimeType)) thrownew ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "target"); if (method ==null) thrownew ArgumentNullException("method"); Type c = type.BaseType; if (c ==null|| (c !=typeof(Delegate) && c !=typeof(MulticastDelegate))) thrownew ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); Delegate d = InternalAlloc((RuntimeType)type); d.InternalCreateStatic((RuntimeType)target,method); return d; } [MethodImplAttribute(MethodImplOptions.InternalCall)] privateexternvoid NeverCallThis(Object target, IntPtr slot); /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.CreateDelegate2"]/*' /> publicstatic Delegate CreateDelegate(Type type,MethodInfo method) { // Validate the parameters. if (type ==null) thrownew ArgumentNullException("type"); if (!(type is RuntimeType)) thrownew ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type"); if (method ==null) thrownew ArgumentNullException("method"); if (!(method is RuntimeMethodInfo)) thrownew ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "method"); Type c = type.BaseType; if (c ==null|| (c !=typeof(Delegate) && c !=typeof(MulticastDelegate))) thrownew ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); if (!method.IsStatic) thrownew ArgumentException(Environment.GetResourceString("Arg_MustBeStatic"),"method"); // Find the Invoke method MethodInfo m = type.GetMethod("Invoke"); if (m ==null) thrownew InvalidProgramException("Didn't find Delegate Invoke method."); if (!(m is RuntimeMethodInfo)) thrownew ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "type.Invoke"); // Initialize the method Delegate d = InternalAlloc((RuntimeType)type); d.InternalCreateMethod((RuntimeMethodInfo)m,(RuntimeMethodInfo)method); return d; } // Big cheat to get the internal delegate. [MethodImplAttribute(MethodImplOptions.InternalCall)] privateexternstatic Delegate InternalAlloc(RuntimeType type); // This method is the internal native method that initializes the // the delegate. It will initialize all of the internal fields // above. [MethodImplAttribute(MethodImplOptions.InternalCall)] internalexternvoid InternalCreate(Object target, String method, bool ignoreCase); // Internal create for a static method [MethodImplAttribute(MethodImplOptions.InternalCall)] internalexternvoid InternalCreateStatic(RuntimeType target, String method); // Internal create for a static method [MethodImplAttribute(MethodImplOptions.InternalCall)] internalexternvoid InternalCreateMethod(RuntimeMethodInfo invokeMeth,RuntimeMethodInfo targetMethod); // Internal method to get the reflection method info [MethodImplAttribute(MethodImplOptions.InternalCall)] internalextern RuntimeMethodInfo InternalFindMethodInfo(); // InternalCreateMethod will create a delegate based upon the MethodInfo.. // The method must be static /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.operatorEQ"]/*' /> publicstaticbooloperator==(Delegate d1, Delegate d2) { if ((Object)d1 ==null) return (Object)d2 ==null; return d1.Equals(d2); } /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.operatorNE"]/*' /> publicstaticbooloperator!= (Delegate d1, Delegate d2) { if ((Object)d1 ==null) return (Object)d2 !=null; return!d1.Equals(d2); } // // Implementation of ISerializable // /**////<include file='doc\Delegate.uex' path='docs/doc[@for="Delegate.GetObjectData"]/*' /> publicvirtualvoid GetObjectData(SerializationInfo info, StreamingContext context) { if (!IsStatic()) { DelegateSerializationHolder.GetDelegateSerializationInfo(info, this.GetType(), _target, Method, 0); }else{ DelegateSerializationHolder.GetDelegateSerializationInfo(info, this.GetType(), null, Method, 0); } } // // This is just designed to prevent compiler warnings. // This field is used from native, but we need to prevent the compiler warnings. // #if _DEBUG privatevoid DontTouchThis() { _methodPtr = IntPtr.Zero; _target =null; } #endif } }
// ==++== // // // Copyright (c) 2002 Microsoft Corporation. All rights reserved. // // The use and distribution terms for this software are contained in the file // named license.txt, which can be found in the root of this distribution. // By using this software in any fashion, you are agreeing to be bound by the // terms of this license. // // You must not remove this notice, or any other, from this software. // // // ==--== /**//*============================================================ ** ** Class: Int32 ** ** ** ** Purpose: A representation of a 32 bit 2's complement ** integer. ** ** Date: July 23, 1998 ** ===========================================================*/ namespace System { using System; using System.Globalization; using System.Runtime.InteropServices; /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32"]/*' /> [Serializable, System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)] publicstruct Int32 : IComparable, IFormattable, IConvertible { internalint m_value; /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.MaxValue"]/*' /> publicconstint MaxValue =0x7fffffff; /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.MinValue"]/*' /> publicconstint MinValue =unchecked((int)0x80000000); // Compares this object to another object, returning an integer that // indicates the relationship. // Returns a value less than zero if this object // null is considered to be less than any instance. // If object is not of type Int32, this method throws an ArgumentException. // /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.CompareTo"]/*' /> publicint CompareTo(Object value) { if (value ==null) { return1; } if (value is Int32) { // Need to use compare because subtraction will wrap // to positive for very large neg numbers, etc. int i = (int)value; if (m_value < i) return-1; if (m_value > i) return1; return0; } thrownew ArgumentException (Environment.GetResourceString("Arg_MustBeInt32")); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.Equals"]/*' /> publicoverridebool Equals(Object obj) { if (!(obj is Int32)) { returnfalse; } return m_value == ((Int32)obj).m_value; } // The absolute value of the int contained. /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.GetHashCode"]/*' /> publicoverrideint GetHashCode() { return m_value; } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.ToString"]/*' /> publicoverride String ToString() { return ToString(null, null); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.ToString1"]/*' /> public String ToString(String format) { return ToString(format, null); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.ToString2"]/*' /> public String ToString(String format, IFormatProvider provider) { return Number.FormatInt32(m_value, format, NumberFormatInfo.GetInstance(provider)); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.Parse"]/*' /> publicstaticint Parse(String s) { return Parse(s, NumberStyles.Integer, null); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.Parse1"]/*' /> publicstaticint Parse(String s, NumberStyles style) { NumberFormatInfo.ValidateParseStyle(style); return Parse(s, style, null); } // Parses an integer from a String in the given style. If // a NumberFormatInfo isn't specified, the current culture's // NumberFormatInfo is assumed. // /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.Parse2"]/*' /> publicstaticint Parse(String s, IFormatProvider provider) { NumberFormatInfo info = NumberFormatInfo.GetInstance(provider); return Number.ParseInt32(s, NumberStyles.Integer, info); } // Parses an integer from a String in the given style. If // a NumberFormatInfo isn't specified, the current culture's // NumberFormatInfo is assumed. // /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.Parse3"]/*' /> publicstaticint Parse(String s, NumberStyles style, IFormatProvider provider) { NumberFormatInfo info = NumberFormatInfo.GetInstance(provider); NumberFormatInfo.ValidateParseStyle(style); return Number.ParseInt32(s, style, info); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.ToString3"]/*' /> public String ToString(IFormatProvider provider) { return ToString(null, provider); } // // IValue implementation // /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.GetTypeCode"]/*' /> public TypeCode GetTypeCode() { return TypeCode.Int32; } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToBoolean"]/*' /> ///<internalonly/> bool IConvertible.ToBoolean(IFormatProvider provider) { return Convert.ToBoolean(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToChar"]/*' /> ///<internalonly/> char IConvertible.ToChar(IFormatProvider provider) { return Convert.ToChar(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToSByte"]/*' /> ///<internalonly/> [CLSCompliant(false)] sbyte IConvertible.ToSByte(IFormatProvider provider) { return Convert.ToSByte(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToByte"]/*' /> ///<internalonly/> byte IConvertible.ToByte(IFormatProvider provider) { return Convert.ToByte(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToInt16"]/*' /> ///<internalonly/> short IConvertible.ToInt16(IFormatProvider provider) { return Convert.ToInt16(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToUInt16"]/*' /> ///<internalonly/> [CLSCompliant(false)] ushort IConvertible.ToUInt16(IFormatProvider provider) { return Convert.ToUInt16(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToInt32"]/*' /> ///<internalonly/> int IConvertible.ToInt32(IFormatProvider provider) { return m_value; } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToUInt32"]/*' /> ///<internalonly/> [CLSCompliant(false)] uint IConvertible.ToUInt32(IFormatProvider provider) { return Convert.ToUInt32(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToInt64"]/*' /> ///<internalonly/> long IConvertible.ToInt64(IFormatProvider provider) { return Convert.ToInt64(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToUInt64"]/*' /> ///<internalonly/> [CLSCompliant(false)] ulong IConvertible.ToUInt64(IFormatProvider provider) { return Convert.ToUInt64(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToSingle"]/*' /> ///<internalonly/> float IConvertible.ToSingle(IFormatProvider provider) { return Convert.ToSingle(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToDouble"]/*' /> ///<internalonly/> double IConvertible.ToDouble(IFormatProvider provider) { return Convert.ToDouble(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToDecimal"]/*' /> ///<internalonly/> Decimal IConvertible.ToDecimal(IFormatProvider provider) { return Convert.ToDecimal(m_value); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToDateTime"]/*' /> ///<internalonly/> DateTime IConvertible.ToDateTime(IFormatProvider provider) { thrownew InvalidCastException(String.Format(Environment.GetResourceString("InvalidCast_FromTo"), "Int32", "DateTime")); } /**////<include file='doc\Int32.uex' path='docs/doc[@for="Int32.IConvertible.ToType"]/*' /> ///<internalonly/> Object IConvertible.ToType(Type type, IFormatProvider provider) { return Convert.DefaultToType((IConvertible)this, type, provider); } // // This is just designed to prevent compiler warnings. // This field is used from native, but we need to prevent the compiler warnings. // #if _DEBUG privatevoid DontTouchThis() { m_value =0; } #endif } }
posted on
2006-03-01 00:01kasafuma
阅读(3617)
评论(10)
收藏举报