Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / CompMod / System / ComponentModel / Design / Serialization / InstanceDescriptor.cs / 1 / InstanceDescriptor.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.ComponentModel.Design.Serialization {
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Security.Permissions;
///
/// EventArgs for the ResolveNameEventHandler. This event is used
/// by the serialization process to match a name to an object
/// instance.
///
[HostProtection(SharedState = true)]
[System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name = "FullTrust")]
public sealed class InstanceDescriptor {
private MemberInfo member;
private ICollection arguments;
private bool isComplete;
///
/// Creates a new InstanceDescriptor.
///
public InstanceDescriptor(MemberInfo member, ICollection arguments) : this(member, arguments, true) {
}
///
/// Creates a new InstanceDescriptor.
///
public InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete) {
this.member = member;
this.isComplete = isComplete;
if (arguments == null) {
this.arguments = new object[0];
}
else {
object[] args = new object[arguments.Count];
arguments.CopyTo(args, 0);
this.arguments = args;
}
if (member is FieldInfo) {
FieldInfo fi = (FieldInfo)member;
if (!fi.IsStatic) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeStatic));
}
if (this.arguments.Count != 0) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorLengthMismatch));
}
}
else if (member is ConstructorInfo) {
ConstructorInfo ci = (ConstructorInfo)member;
if (ci.IsStatic) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorCannotBeStatic));
}
if (this.arguments.Count != ci.GetParameters().Length) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorLengthMismatch));
}
}
else if (member is MethodInfo) {
MethodInfo mi = (MethodInfo)member;
if (!mi.IsStatic) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeStatic));
}
if (this.arguments.Count != mi.GetParameters().Length) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorLengthMismatch));
}
}
else if (member is PropertyInfo) {
PropertyInfo pi = (PropertyInfo)member;
if (!pi.CanRead) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeReadable));
}
MethodInfo mi = pi.GetGetMethod();
if (mi != null && !mi.IsStatic) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeStatic));
}
}
}
///
/// The collection of arguments that should be passed to
/// MemberInfo in order to create an instance.
///
public ICollection Arguments {
get {
return arguments;
}
}
///
/// Determines if the contents of this instance descriptor completely identify the instance.
/// This will normally be the case, but some objects may be too complex for a single method
/// or constructor to represent. IsComplete can be used to identify these objects and take
/// additional steps to further describe their state.
///
public bool IsComplete {
get {
return isComplete;
}
}
///
/// The MemberInfo object that was passed into the constructor
/// of this InstanceDescriptor.
///
public MemberInfo MemberInfo {
get {
return member;
}
}
///
/// Invokes this instance descriptor, returning the object
/// the descriptor describes.
///
public object Invoke() {
object[] translatedArguments = new object[arguments.Count];
arguments.CopyTo(translatedArguments, 0);
// Instance descriptors can contain other instance
// descriptors. Translate them if necessary.
//
for(int i = 0; i < translatedArguments.Length; i++) {
if (translatedArguments[i] is InstanceDescriptor) {
translatedArguments[i] = ((InstanceDescriptor)translatedArguments[i]).Invoke();
}
}
if (member is ConstructorInfo) {
return ((ConstructorInfo)member).Invoke(translatedArguments);
}
else if (member is MethodInfo) {
return ((MethodInfo)member).Invoke(null, translatedArguments);
}
else if (member is PropertyInfo) {
return ((PropertyInfo)member).GetValue(null, translatedArguments);
}
else if (member is FieldInfo) {
return ((FieldInfo)member).GetValue(null);
}
else {
Debug.Fail("Unrecognized reflection type in instance descriptor: " + member.GetType().Name);
}
return null;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.ComponentModel.Design.Serialization {
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Security.Permissions;
///
/// EventArgs for the ResolveNameEventHandler. This event is used
/// by the serialization process to match a name to an object
/// instance.
///
[HostProtection(SharedState = true)]
[System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name = "FullTrust")]
public sealed class InstanceDescriptor {
private MemberInfo member;
private ICollection arguments;
private bool isComplete;
///
/// Creates a new InstanceDescriptor.
///
public InstanceDescriptor(MemberInfo member, ICollection arguments) : this(member, arguments, true) {
}
///
/// Creates a new InstanceDescriptor.
///
public InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete) {
this.member = member;
this.isComplete = isComplete;
if (arguments == null) {
this.arguments = new object[0];
}
else {
object[] args = new object[arguments.Count];
arguments.CopyTo(args, 0);
this.arguments = args;
}
if (member is FieldInfo) {
FieldInfo fi = (FieldInfo)member;
if (!fi.IsStatic) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeStatic));
}
if (this.arguments.Count != 0) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorLengthMismatch));
}
}
else if (member is ConstructorInfo) {
ConstructorInfo ci = (ConstructorInfo)member;
if (ci.IsStatic) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorCannotBeStatic));
}
if (this.arguments.Count != ci.GetParameters().Length) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorLengthMismatch));
}
}
else if (member is MethodInfo) {
MethodInfo mi = (MethodInfo)member;
if (!mi.IsStatic) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeStatic));
}
if (this.arguments.Count != mi.GetParameters().Length) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorLengthMismatch));
}
}
else if (member is PropertyInfo) {
PropertyInfo pi = (PropertyInfo)member;
if (!pi.CanRead) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeReadable));
}
MethodInfo mi = pi.GetGetMethod();
if (mi != null && !mi.IsStatic) {
throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeStatic));
}
}
}
///
/// The collection of arguments that should be passed to
/// MemberInfo in order to create an instance.
///
public ICollection Arguments {
get {
return arguments;
}
}
///
/// Determines if the contents of this instance descriptor completely identify the instance.
/// This will normally be the case, but some objects may be too complex for a single method
/// or constructor to represent. IsComplete can be used to identify these objects and take
/// additional steps to further describe their state.
///
public bool IsComplete {
get {
return isComplete;
}
}
///
/// The MemberInfo object that was passed into the constructor
/// of this InstanceDescriptor.
///
public MemberInfo MemberInfo {
get {
return member;
}
}
///
/// Invokes this instance descriptor, returning the object
/// the descriptor describes.
///
public object Invoke() {
object[] translatedArguments = new object[arguments.Count];
arguments.CopyTo(translatedArguments, 0);
// Instance descriptors can contain other instance
// descriptors. Translate them if necessary.
//
for(int i = 0; i < translatedArguments.Length; i++) {
if (translatedArguments[i] is InstanceDescriptor) {
translatedArguments[i] = ((InstanceDescriptor)translatedArguments[i]).Invoke();
}
}
if (member is ConstructorInfo) {
return ((ConstructorInfo)member).Invoke(translatedArguments);
}
else if (member is MethodInfo) {
return ((MethodInfo)member).Invoke(null, translatedArguments);
}
else if (member is PropertyInfo) {
return ((PropertyInfo)member).GetValue(null, translatedArguments);
}
else if (member is FieldInfo) {
return ((FieldInfo)member).GetValue(null);
}
else {
Debug.Fail("Unrecognized reflection type in instance descriptor: " + member.GetType().Name);
}
return null;
}
}
}
// 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
- Pointer.cs
- DataSpaceManager.cs
- Root.cs
- CryptoStream.cs
- Context.cs
- Rect.cs
- UpdatableWrapper.cs
- ParserOptions.cs
- TraceSwitch.cs
- _NestedMultipleAsyncResult.cs
- HtmlInputFile.cs
- TemplateField.cs
- PointLight.cs
- EncryptedKeyIdentifierClause.cs
- ExeConfigurationFileMap.cs
- LinqDataView.cs
- HasCopySemanticsAttribute.cs
- NullReferenceException.cs
- TaskFormBase.cs
- AppSettingsExpressionBuilder.cs
- TextEditorContextMenu.cs
- VScrollBar.cs
- ExpressionBinding.cs
- DataGridViewRowsAddedEventArgs.cs
- StoragePropertyMapping.cs
- BypassElement.cs
- ResolvedKeyFrameEntry.cs
- SmtpMail.cs
- HijriCalendar.cs
- unitconverter.cs
- ZoneMembershipCondition.cs
- UnknownBitmapEncoder.cs
- BlobPersonalizationState.cs
- MetadataAssemblyHelper.cs
- AuthenticationModuleElement.cs
- SQLString.cs
- TimeoutHelper.cs
- EdmToObjectNamespaceMap.cs
- ReceiveContext.cs
- SqlDataSourceTableQuery.cs
- CacheChildrenQuery.cs
- CodeGeneratorOptions.cs
- SectionXmlInfo.cs
- HostAdapter.cs
- ListViewHitTestInfo.cs
- OracleLob.cs
- XPathException.cs
- EditorBrowsableAttribute.cs
- ListViewInsertedEventArgs.cs
- XmlHierarchicalDataSourceView.cs
- BooleanConverter.cs
- CatalogZone.cs
- UriTemplateDispatchFormatter.cs
- CorePropertiesFilter.cs
- EntityDescriptor.cs
- SqlUtil.cs
- EventSource.cs
- HandleExceptionArgs.cs
- EntityDataSourceWrapper.cs
- Action.cs
- DataBoundLiteralControl.cs
- BasePattern.cs
- CalendarSelectionChangedEventArgs.cs
- ImplicitInputBrush.cs
- UiaCoreApi.cs
- TableItemPatternIdentifiers.cs
- HttpEncoderUtility.cs
- WebScriptMetadataMessage.cs
- ObjectTag.cs
- EventSourceCreationData.cs
- XsdCachingReader.cs
- DynamicRouteExpression.cs
- Encoding.cs
- ProviderConnectionPointCollection.cs
- DetailsViewPagerRow.cs
- DataGridViewCellCollection.cs
- TextDocumentView.cs
- XmlLanguage.cs
- Converter.cs
- GeneralTransform3DTo2DTo3D.cs
- UIElementHelper.cs
- StylusPointPropertyInfoDefaults.cs
- XmlSerializerSection.cs
- Normalization.cs
- PenThreadPool.cs
- SizeF.cs
- GeometryHitTestParameters.cs
- TagPrefixCollection.cs
- FixUpCollection.cs
- ConnectionDemuxer.cs
- TextStore.cs
- DataBindingExpressionBuilder.cs
- BaseTreeIterator.cs
- InputDevice.cs
- RegexCompilationInfo.cs
- WebConfigurationManager.cs
- NameValuePermission.cs
- ListControlStringCollectionEditor.cs
- Compensate.cs
- CaseInsensitiveOrdinalStringComparer.cs