Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
 internal static object SerializeResourceType(object clrObject, ResourceType resourceType, int depth)
 {
     return(SerializerBase.CreateSerializer(resourceType).Serialize(clrObject, depth));
 }