Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / clr / src / BCL / System / TypedReference.cs / 1 / TypedReference.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
namespace System {
// TypedReference is basically only ever seen on the call stack, and in param arrays.
// These are blob that must be dealt with by the compiler.
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using CultureInfo = System.Globalization.CultureInfo;
using FieldInfo = System.Reflection.FieldInfo;
using System.Security.Permissions;
[CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(true)]
public struct TypedReference
{
private IntPtr Value;
private IntPtr Type;
[CLSCompliant(false)]
[ReflectionPermission(SecurityAction.LinkDemand, MemberAccess=true)]
public static TypedReference MakeTypedReference(Object target, FieldInfo[] flds) {
if (target == null)
throw new ArgumentNullException("target");
if (flds == null)
throw new ArgumentNullException("flds");
if (flds.Length == 0)
throw new ArgumentException(Environment.GetResourceString("Arg_ArrayZeroError"));
else {
RuntimeFieldHandle[] fields = new RuntimeFieldHandle[flds.Length];
// For proper handling of Nullable don't change GetType() to something like 'IsAssignableFrom'
// Currently we can't make a TypedReference to fields of Nullable, which is fine.
Type targetType = target.GetType();
for (int i = 0; i < flds.Length; i++) {
FieldInfo field = flds[i];
if (!(field is RuntimeFieldInfo))
throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeFieldInfo"));
else if (field.IsInitOnly || field.IsStatic)
throw new ArgumentException(Environment.GetResourceString("Argument_TypedReferenceInvalidField"));
if (targetType != field.DeclaringType && !targetType.IsSubclassOf(field.DeclaringType))
throw new MissingMemberException(Environment.GetResourceString("MissingMemberTypeRef"));
Type fieldType = field.FieldType;
if (fieldType.IsPrimitive)
throw new ArgumentException(Environment.GetResourceString("Arg_TypeRefPrimitve"));
if (i < flds.Length - 1)
if (!fieldType.IsValueType)
throw new MissingMemberException(Environment.GetResourceString("MissingMemberNestErr"));
fields[i] = field.FieldHandle;
targetType = fieldType;
}
TypedReference result = new TypedReference ();
// reference to TypedReference is banned, so have to pass result as pointer
unsafe
{
InternalMakeTypedReference(&result, target, fields, targetType.TypeHandle);
}
return result;
}
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
// reference to TypedReference is banned, so have to pass result as pointer
private unsafe static extern void InternalMakeTypedReference(void * result, Object target, RuntimeFieldHandle[] flds, RuntimeTypeHandle lastFieldType);
public override int GetHashCode()
{
if (Type == IntPtr.Zero)
return 0;
else
return __reftype(this).GetHashCode();
}
public override bool Equals(Object o)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_NYI"));
}
public unsafe static Object ToObject(TypedReference value)
{
return InternalToObject(&value);
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal unsafe extern static Object InternalToObject(void * value);
internal bool IsNull
{
get
{
return Value.IsNull() && Type.IsNull();
}
}
public static Type GetTargetType (TypedReference value)
{
return __reftype(value);
}
public static RuntimeTypeHandle TargetTypeToken (TypedReference value)
{
return __reftype(value).TypeHandle;
}
// This may cause the type to be changed.
[CLSCompliant(false)]
public unsafe static void SetTypedReference(TypedReference target, Object value)
{
InternalSetTypedReference(&target, value);
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal unsafe extern static void InternalSetTypedReference(void * target, Object value);
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
namespace System {
// TypedReference is basically only ever seen on the call stack, and in param arrays.
// These are blob that must be dealt with by the compiler.
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using CultureInfo = System.Globalization.CultureInfo;
using FieldInfo = System.Reflection.FieldInfo;
using System.Security.Permissions;
[CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(true)]
public struct TypedReference
{
private IntPtr Value;
private IntPtr Type;
[CLSCompliant(false)]
[ReflectionPermission(SecurityAction.LinkDemand, MemberAccess=true)]
public static TypedReference MakeTypedReference(Object target, FieldInfo[] flds) {
if (target == null)
throw new ArgumentNullException("target");
if (flds == null)
throw new ArgumentNullException("flds");
if (flds.Length == 0)
throw new ArgumentException(Environment.GetResourceString("Arg_ArrayZeroError"));
else {
RuntimeFieldHandle[] fields = new RuntimeFieldHandle[flds.Length];
// For proper handling of Nullable don't change GetType() to something like 'IsAssignableFrom'
// Currently we can't make a TypedReference to fields of Nullable, which is fine.
Type targetType = target.GetType();
for (int i = 0; i < flds.Length; i++) {
FieldInfo field = flds[i];
if (!(field is RuntimeFieldInfo))
throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeFieldInfo"));
else if (field.IsInitOnly || field.IsStatic)
throw new ArgumentException(Environment.GetResourceString("Argument_TypedReferenceInvalidField"));
if (targetType != field.DeclaringType && !targetType.IsSubclassOf(field.DeclaringType))
throw new MissingMemberException(Environment.GetResourceString("MissingMemberTypeRef"));
Type fieldType = field.FieldType;
if (fieldType.IsPrimitive)
throw new ArgumentException(Environment.GetResourceString("Arg_TypeRefPrimitve"));
if (i < flds.Length - 1)
if (!fieldType.IsValueType)
throw new MissingMemberException(Environment.GetResourceString("MissingMemberNestErr"));
fields[i] = field.FieldHandle;
targetType = fieldType;
}
TypedReference result = new TypedReference ();
// reference to TypedReference is banned, so have to pass result as pointer
unsafe
{
InternalMakeTypedReference(&result, target, fields, targetType.TypeHandle);
}
return result;
}
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
// reference to TypedReference is banned, so have to pass result as pointer
private unsafe static extern void InternalMakeTypedReference(void * result, Object target, RuntimeFieldHandle[] flds, RuntimeTypeHandle lastFieldType);
public override int GetHashCode()
{
if (Type == IntPtr.Zero)
return 0;
else
return __reftype(this).GetHashCode();
}
public override bool Equals(Object o)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_NYI"));
}
public unsafe static Object ToObject(TypedReference value)
{
return InternalToObject(&value);
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal unsafe extern static Object InternalToObject(void * value);
internal bool IsNull
{
get
{
return Value.IsNull() && Type.IsNull();
}
}
public static Type GetTargetType (TypedReference value)
{
return __reftype(value);
}
public static RuntimeTypeHandle TargetTypeToken (TypedReference value)
{
return __reftype(value).TypeHandle;
}
// This may cause the type to be changed.
[CLSCompliant(false)]
public unsafe static void SetTypedReference(TypedReference target, Object value)
{
InternalSetTypedReference(&target, value);
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal unsafe extern static void InternalSetTypedReference(void * target, Object value);
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- CategoryAttribute.cs
- CallbackDebugBehavior.cs
- WindowsTooltip.cs
- WorkflowInstanceContextProvider.cs
- BCryptNative.cs
- SafeProcessHandle.cs
- CodePrimitiveExpression.cs
- ImpersonationContext.cs
- HelloOperationCD1AsyncResult.cs
- EventWaitHandle.cs
- HandoffBehavior.cs
- SettingsPropertyCollection.cs
- XmlBinaryWriterSession.cs
- ACL.cs
- Enum.cs
- CreateUserWizardStep.cs
- MenuItem.cs
- PointCollection.cs
- AssertUtility.cs
- entityreference_tresulttype.cs
- TableAdapterManagerGenerator.cs
- CapabilitiesSection.cs
- FrameworkPropertyMetadata.cs
- NonBatchDirectoryCompiler.cs
- DataGridViewUtilities.cs
- WebPartConnectionsCloseVerb.cs
- ExpressionBindings.cs
- Underline.cs
- HyperLinkColumn.cs
- COM2ComponentEditor.cs
- XPathDocumentIterator.cs
- _ConnectStream.cs
- UpdateCommandGenerator.cs
- InsufficientMemoryException.cs
- MULTI_QI.cs
- ApplicationId.cs
- DataSetUtil.cs
- BindingExpressionBase.cs
- LogReserveAndAppendState.cs
- ArgumentNullException.cs
- LightweightCodeGenerator.cs
- ConfigXmlSignificantWhitespace.cs
- Pair.cs
- ColorKeyFrameCollection.cs
- InvalidComObjectException.cs
- ClientTargetSection.cs
- _FtpDataStream.cs
- DataMemberConverter.cs
- Point3DCollectionConverter.cs
- VisualTarget.cs
- SortKey.cs
- ClientWindowsAuthenticationMembershipProvider.cs
- LayoutSettings.cs
- IPeerNeighbor.cs
- SiteOfOriginContainer.cs
- ResourcePool.cs
- ZipIOExtraFieldPaddingElement.cs
- DataGridViewTextBoxColumn.cs
- XsltSettings.cs
- FormsAuthenticationTicket.cs
- XamlInt32CollectionSerializer.cs
- MsmqIntegrationChannelFactory.cs
- SplineKeyFrames.cs
- EmissiveMaterial.cs
- SectionUpdates.cs
- ADMembershipProvider.cs
- WindowsAltTab.cs
- DynamicValidatorEventArgs.cs
- Comparer.cs
- StatusBarItem.cs
- PeerNearMe.cs
- TableLayoutSettingsTypeConverter.cs
- ToolStripDropDownButton.cs
- ConfigXmlDocument.cs
- ThreadStateException.cs
- HtmlGenericControl.cs
- ViewSimplifier.cs
- AndAlso.cs
- Size.cs
- BulletedListEventArgs.cs
- GroupBox.cs
- Figure.cs
- RemoteEndpointMessageProperty.cs
- RewritingSimplifier.cs
- FormsAuthentication.cs
- DoubleIndependentAnimationStorage.cs
- WpfKnownMemberInvoker.cs
- CodeLabeledStatement.cs
- MSG.cs
- DbException.cs
- ArrayHelper.cs
- StringCollectionEditor.cs
- EncoderExceptionFallback.cs
- SoapFaultCodes.cs
- MatrixUtil.cs
- FacetChecker.cs
- AxHostDesigner.cs
- XhtmlBasicTextViewAdapter.cs
- DiscoveryDefaults.cs
- GestureRecognizer.cs