public override object Serialize(object clrObject, int depth) { object value; object[] name = new object[1]; name[0] = base.ResourceType.Name; clrObject.ThrowIfNull("clrObject", new ParameterExtensions.MessageLoader(SerializerBase.GetNullPassedForSerializingEntityMessage), name); ResourceType resourceType = base.ResourceType; if (clrObject as PSObject == null) { resourceType = base.ResourceType.FindResourceType(clrObject.GetType()); } else { PSObject pSObject = clrObject as PSObject; if (pSObject != null && pSObject.BaseObject != null) { resourceType = base.ResourceType.FindResourceType(pSObject.BaseObject.GetType()); } } DSResource dSResource = new DSResource(resourceType, this.serializeKeyOnly); foreach (ResourceProperty property in resourceType.Properties) { if (this.serializeKeyOnly && (property.Kind & ResourcePropertyKind.Key) != ResourcePropertyKind.Key) { continue; } if ((property.Kind & ResourcePropertyKind.ResourceSetReference) == ResourcePropertyKind.ResourceSetReference) { PSEntityMetadata testHookEntityMetadata = this.TestHookEntityMetadata; if (testHookEntityMetadata == null) { DataContext currentContext = DataServiceController.Current.GetCurrentContext(); if (currentContext != null) { testHookEntityMetadata = currentContext.UserSchema.GetEntityMetadata(base.ResourceType) as PSEntityMetadata; } } if (testHookEntityMetadata != null) { PSEntityMetadata.ReferenceSetCmdlets referenceSetCmdlet = null; if (testHookEntityMetadata.CmdletsForReferenceSets.TryGetValue(property.Name, out referenceSetCmdlet) && referenceSetCmdlet.Cmdlets.ContainsKey(CommandType.GetReference)) { if (referenceSetCmdlet.GetRefHidden) { dSResource.SetValue(property.Name, null); continue; } else { PSReferencedResourceSet pSReferencedResourceSet = new PSReferencedResourceSet(property, base.ResourceType); dSResource.SetValue(property.Name, pSReferencedResourceSet); continue; } } } } if (clrObject != null) { value = SerializerBase.GetValue(property, clrObject); } else { value = null; } object obj = value; if (obj == null) { if (!property.ResourceType.IsPrimitive() || property.ResourceType.IsNullable()) { if ((property.Kind & (ResourcePropertyKind.Primitive | ResourcePropertyKind.ResourceReference)) != 0) { Tracer tracer = new Tracer(); tracer.DebugMessage(string.Concat(property.Name, " is null; skipping")); continue; } } else { object[] objArray = new object[1]; objArray[0] = property.Name; throw new PSObjectSerializationFailedException(string.Format(CultureInfo.CurrentCulture, Resources.PropertyNotFoundInPSObject, objArray)); } } dSResource.SetValue(property.Name, SerializerBase.SerializeResourceProperty(obj, base.ResourceType, property, depth)); } return dSResource; }
public static DSResource CreateResourceWithKeyAndReferenceSetCmdlets(ResourceType resourceType, Dictionary<string, object> keyProperties, EntityMetadata entityMetadata) { DSResource dSResource = ResourceTypeExtensions.CreateKeyOnlyResource(resourceType, keyProperties); if (dSResource != null) { PSEntityMetadata pSEntityMetadatum = entityMetadata as PSEntityMetadata; ReadOnlyCollection<ResourceProperty> properties = resourceType.Properties; foreach (ResourceProperty resourceProperty in properties.Where<ResourceProperty>((ResourceProperty it) => (it.Kind & ResourcePropertyKind.ResourceSetReference) == ResourcePropertyKind.ResourceSetReference)) { PSEntityMetadata.ReferenceSetCmdlets referenceSetCmdlet = null; if (!pSEntityMetadatum.CmdletsForReferenceSets.TryGetValue(resourceProperty.Name, out referenceSetCmdlet) || !referenceSetCmdlet.Cmdlets.ContainsKey(CommandType.GetReference)) { continue; } if (referenceSetCmdlet.GetRefHidden) { dSResource.SetValue(resourceProperty.Name, null); } else { PSReferencedResourceSet pSReferencedResourceSet = new PSReferencedResourceSet(resourceProperty, resourceType); dSResource.SetValue(resourceProperty.Name, pSReferencedResourceSet); } } return dSResource; } else { return null; } }
public override object Serialize(object clrObject, int depth) { object value; object[] name = new object[1]; name[0] = base.ResourceType.Name; clrObject.ThrowIfNull("clrObject", new ParameterExtensions.MessageLoader(SerializerBase.GetNullPassedForSerializingEntityMessage), name); ResourceType resourceType = base.ResourceType; if (clrObject as PSObject == null) { resourceType = base.ResourceType.FindResourceType(clrObject.GetType()); } else { PSObject pSObject = clrObject as PSObject; if (pSObject != null && pSObject.BaseObject != null) { resourceType = base.ResourceType.FindResourceType(pSObject.BaseObject.GetType()); } } DSResource dSResource = new DSResource(resourceType, this.serializeKeyOnly); foreach (ResourceProperty property in resourceType.Properties) { if (this.serializeKeyOnly && (property.Kind & ResourcePropertyKind.Key) != ResourcePropertyKind.Key) { continue; } if ((property.Kind & ResourcePropertyKind.ResourceSetReference) == ResourcePropertyKind.ResourceSetReference) { PSEntityMetadata testHookEntityMetadata = this.TestHookEntityMetadata; if (testHookEntityMetadata == null) { DataContext currentContext = DataServiceController.Current.GetCurrentContext(); if (currentContext != null) { testHookEntityMetadata = currentContext.UserSchema.GetEntityMetadata(base.ResourceType) as PSEntityMetadata; } } if (testHookEntityMetadata != null) { PSEntityMetadata.ReferenceSetCmdlets referenceSetCmdlet = null; if (testHookEntityMetadata.CmdletsForReferenceSets.TryGetValue(property.Name, out referenceSetCmdlet) && referenceSetCmdlet.Cmdlets.ContainsKey(CommandType.GetReference)) { if (referenceSetCmdlet.GetRefHidden) { dSResource.SetValue(property.Name, null); continue; } else { PSReferencedResourceSet pSReferencedResourceSet = new PSReferencedResourceSet(property, base.ResourceType); dSResource.SetValue(property.Name, pSReferencedResourceSet); continue; } } } } if (clrObject != null) { value = SerializerBase.GetValue(property, clrObject); } else { value = null; } object obj = value; if (obj == null) { if (!property.ResourceType.IsPrimitive() || property.ResourceType.IsNullable()) { if ((property.Kind & (ResourcePropertyKind.Primitive | ResourcePropertyKind.ResourceReference)) != 0) { Tracer tracer = new Tracer(); tracer.DebugMessage(string.Concat(property.Name, " is null; skipping")); continue; } } else { object[] objArray = new object[1]; objArray[0] = property.Name; throw new PSObjectSerializationFailedException(string.Format(CultureInfo.CurrentCulture, Resources.PropertyNotFoundInPSObject, objArray)); } } dSResource.SetValue(property.Name, SerializerBase.SerializeResourceProperty(obj, base.ResourceType, property, depth)); } return(dSResource); }