mscorlib(4.0.0.0) API with additions
RuntimeFieldHandle.cs
1 using System.Reflection;
6 using System.Security;
7 
8 namespace System
9 {
11  [Serializable]
12  [ComVisible(true)]
13  [__DynamicallyInvokable]
15  {
16  private IRuntimeFieldInfo m_ptr;
17 
20  public IntPtr Value
21  {
22  [SecurityCritical]
23  get
24  {
25  if (m_ptr == null)
26  {
27  return IntPtr.Zero;
28  }
29  return m_ptr.Value.Value;
30  }
31  }
32 
33  internal RuntimeFieldHandle GetNativeHandle()
34  {
35  IRuntimeFieldInfo ptr = m_ptr;
36  if (ptr == null)
37  {
38  throw new ArgumentNullException(null, Environment.GetResourceString("Arg_InvalidHandle"));
39  }
40  return new RuntimeFieldHandle(ptr);
41  }
42 
43  internal RuntimeFieldHandle(IRuntimeFieldInfo fieldInfo)
44  {
45  m_ptr = fieldInfo;
46  }
47 
48  internal IRuntimeFieldInfo GetRuntimeFieldInfo()
49  {
50  return m_ptr;
51  }
52 
53  internal bool IsNullHandle()
54  {
55  return m_ptr == null;
56  }
57 
60  [SecuritySafeCritical]
61  [__DynamicallyInvokable]
62  public override int GetHashCode()
63  {
64  return ValueType.GetHashCodeOfPtr(Value);
65  }
66 
71  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
72  [SecuritySafeCritical]
73  [__DynamicallyInvokable]
74  public override bool Equals(object obj)
75  {
76  if (!(obj is RuntimeFieldHandle))
77  {
78  return false;
79  }
80  return ((RuntimeFieldHandle)obj).Value == Value;
81  }
82 
87  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
88  [SecuritySafeCritical]
89  [__DynamicallyInvokable]
90  public bool Equals(RuntimeFieldHandle handle)
91  {
92  return handle.Value == Value;
93  }
94 
100  [__DynamicallyInvokable]
101  public static bool operator ==(RuntimeFieldHandle left, RuntimeFieldHandle right)
102  {
103  return left.Equals(right);
104  }
105 
111  [__DynamicallyInvokable]
112  public static bool operator !=(RuntimeFieldHandle left, RuntimeFieldHandle right)
113  {
114  return !left.Equals(right);
115  }
116 
117  [MethodImpl(MethodImplOptions.InternalCall)]
118  [SecurityCritical]
119  internal static extern string GetName(RtFieldInfo field);
120 
121  [MethodImpl(MethodImplOptions.InternalCall)]
122  [SecurityCritical]
123  private unsafe static extern void* _GetUtf8Name(RuntimeFieldHandleInternal field);
124 
125  [SecuritySafeCritical]
126  internal unsafe static Utf8String GetUtf8Name(RuntimeFieldHandleInternal field)
127  {
128  return new Utf8String(_GetUtf8Name(field));
129  }
130 
131  [MethodImpl(MethodImplOptions.InternalCall)]
132  [SecurityCritical]
133  internal static extern bool MatchesNameHash(RuntimeFieldHandleInternal handle, uint hash);
134 
135  [MethodImpl(MethodImplOptions.InternalCall)]
136  [SecurityCritical]
137  internal static extern FieldAttributes GetAttributes(RuntimeFieldHandleInternal field);
138 
139  [MethodImpl(MethodImplOptions.InternalCall)]
140  [SecurityCritical]
141  internal static extern RuntimeType GetApproxDeclaringType(RuntimeFieldHandleInternal field);
142 
143  [SecurityCritical]
144  internal static RuntimeType GetApproxDeclaringType(IRuntimeFieldInfo field)
145  {
146  RuntimeType approxDeclaringType = GetApproxDeclaringType(field.Value);
147  GC.KeepAlive(field);
148  return approxDeclaringType;
149  }
150 
151  [MethodImpl(MethodImplOptions.InternalCall)]
152  [SecurityCritical]
153  internal static extern int GetToken(RtFieldInfo field);
154 
155  [MethodImpl(MethodImplOptions.InternalCall)]
156  [SecurityCritical]
157  internal static extern object GetValue(RtFieldInfo field, object instance, RuntimeType fieldType, RuntimeType declaringType, ref bool domainInitialized);
158 
159  [MethodImpl(MethodImplOptions.InternalCall)]
160  [SecurityCritical]
161  internal unsafe static extern object GetValueDirect(RtFieldInfo field, RuntimeType fieldType, void* pTypedRef, RuntimeType contextType);
162 
163  [MethodImpl(MethodImplOptions.InternalCall)]
164  [SecurityCritical]
165  internal static extern void SetValue(RtFieldInfo field, object obj, object value, RuntimeType fieldType, FieldAttributes fieldAttr, RuntimeType declaringType, ref bool domainInitialized);
166 
167  [MethodImpl(MethodImplOptions.InternalCall)]
168  [SecurityCritical]
169  internal unsafe static extern void SetValueDirect(RtFieldInfo field, RuntimeType fieldType, void* pTypedRef, object value, RuntimeType contextType);
170 
171  [MethodImpl(MethodImplOptions.InternalCall)]
172  [SecurityCritical]
173  internal static extern RuntimeFieldHandleInternal GetStaticFieldForGenericType(RuntimeFieldHandleInternal field, RuntimeType declaringType);
174 
175  [MethodImpl(MethodImplOptions.InternalCall)]
176  [SecurityCritical]
177  internal static extern bool AcquiresContextFromThis(RuntimeFieldHandleInternal field);
178 
179  [DllImport("QCall", CharSet = CharSet.Unicode)]
180  [SecurityCritical]
181  [SuppressUnmanagedCodeSecurity]
182  [return: MarshalAs(UnmanagedType.Bool)]
183  private static extern bool IsSecurityCritical(RuntimeFieldHandle fieldHandle);
184 
185  [SecuritySafeCritical]
186  internal bool IsSecurityCritical()
187  {
188  return IsSecurityCritical(GetNativeHandle());
189  }
190 
191  [DllImport("QCall", CharSet = CharSet.Unicode)]
192  [SecurityCritical]
193  [SuppressUnmanagedCodeSecurity]
194  [return: MarshalAs(UnmanagedType.Bool)]
195  private static extern bool IsSecuritySafeCritical(RuntimeFieldHandle fieldHandle);
196 
197  [SecuritySafeCritical]
198  internal bool IsSecuritySafeCritical()
199  {
200  return IsSecuritySafeCritical(GetNativeHandle());
201  }
202 
203  [DllImport("QCall", CharSet = CharSet.Unicode)]
204  [SecurityCritical]
205  [SuppressUnmanagedCodeSecurity]
206  [return: MarshalAs(UnmanagedType.Bool)]
207  private static extern bool IsSecurityTransparent(RuntimeFieldHandle fieldHandle);
208 
209  [SecuritySafeCritical]
210  internal bool IsSecurityTransparent()
211  {
212  return IsSecurityTransparent(GetNativeHandle());
213  }
214 
215  [DllImport("QCall", CharSet = CharSet.Unicode)]
216  [SecurityCritical]
217  [SuppressUnmanagedCodeSecurity]
218  internal static extern void CheckAttributeAccess(RuntimeFieldHandle fieldHandle, RuntimeModule decoratedTarget);
219 
220  [SecurityCritical]
221  private RuntimeFieldHandle(SerializationInfo info, StreamingContext context)
222  {
223  if (info == null)
224  {
225  throw new ArgumentNullException("info");
226  }
227  FieldInfo fieldInfo = (RuntimeFieldInfo)info.GetValue("FieldObj", typeof(RuntimeFieldInfo));
228  if (fieldInfo == null)
229  {
230  throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
231  }
232  RuntimeFieldHandle fieldHandle = fieldInfo.FieldHandle;
233  m_ptr = fieldHandle.m_ptr;
234  if (m_ptr == null)
235  {
236  throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
237  }
238  }
239 
246  [SecurityCritical]
248  {
249  if (info == null)
250  {
251  throw new ArgumentNullException("info");
252  }
253  if (m_ptr == null)
254  {
255  throw new SerializationException(Environment.GetResourceString("Serialization_InvalidFieldState"));
256  }
257  RuntimeFieldInfo value = (RuntimeFieldInfo)RuntimeType.GetFieldInfo(GetRuntimeFieldInfo());
258  info.AddValue("FieldObj", value, typeof(RuntimeFieldInfo));
259  }
260  }
261 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override bool Equals(object obj)
Indicates whether the current instance is equal to the specified object.
Discovers the attributes of a field and provides access to field metadata.
Definition: FieldInfo.cs:15
Definition: __Canon.cs:3
Represents a field using an internal metadata token.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
abstract RuntimeFieldHandle FieldHandle
Gets a RuntimeFieldHandle, which is a handle to the internal metadata representation of a field.
Definition: FieldInfo.cs:25
UnmanagedType
Identifies how to marshal parameters or fields to unmanaged code.
Definition: UnmanagedType.cs:7
FieldAttributes
Specifies flags that describe the attributes of a field.
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data necessary to deserialize t...
The exception thrown when an error occurs during serialization or deserialization.
IntPtr Value
Gets a handle to the field represented by the current instance.
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
static bool operator !=(RuntimeFieldHandle left, RuntimeFieldHandle right)
Indicates whether two T:System.RuntimeFieldHandle structures are not equal.
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
bool Equals(RuntimeFieldHandle handle)
Indicates whether the current instance is equal to the specified T:System.RuntimeFieldHandle.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
Specifies that the class can be serialized.
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
override int GetHashCode()
Returns the hash code for this instance.
Provides the base class for value types.
Definition: ValueType.cs:12
static bool operator==(RuntimeFieldHandle left, RuntimeFieldHandle right)
Indicates whether two T:System.RuntimeFieldHandle structures are equal.