/// <inheritdoc/>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var attachedReference = AttachedReferenceManager.GetAttachedReference(value);
            var destinationType   = TypeConverterHelper.GetDestinationType(context);

            return(UrlReferenceHelper.CreateReference(destinationType, attachedReference.Id, attachedReference.Url));
        }
Beispiel #2
0
        public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
        {
            if (!AssetReference.TryParse(fromScalar.Value, out var guid, out var location))
            {
                throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode url reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value));
            }

            var urlReference = UrlReferenceHelper.CreateReference(context.Descriptor.Type, guid, location.FullPath);

            return(urlReference);
        }
Beispiel #3
0
 public static IReadOnlyList <Type> GetAssetTypes(Type contentType)
 {
     lock (RegistryLock)
     {
         var currentType = contentType;
         if (UrlReferenceHelper.IsGenericUrlReferenceType(currentType))
         {
             currentType = UrlReferenceHelper.GetTargetContentType(currentType);
         }
         else if (UrlReferenceHelper.IsUrlReferenceType(contentType))
         {
             return(GetPublicTypes().Where(t => IsAssetType(t)).ToList());
         }
         List <Type> assetTypes;
         return(ContentToAssetTypes.TryGetValue(currentType, out assetTypes) ? new List <Type>(assetTypes) : new List <Type>());
     }
 }
        /// <inheritdoc/>
        public override void Serialize(ref T urlReference, ArchiveMode mode, [NotNull] SerializationStream stream)
        {
            if (mode == ArchiveMode.Serialize)
            {
                var attachedReference = AttachedReferenceManager.GetAttachedReference(urlReference);
                if (attachedReference is null)
                {
                    throw new InvalidOperationException("UrlReference does not have an AttachedReference.");
                }

                stream.Write(attachedReference.Id);
                stream.Write(attachedReference.Url);
            }
            else
            {
                var id  = stream.Read <AssetId>();
                var url = stream.ReadString();

                urlReference = (T)UrlReferenceHelper.CreateReference(typeof(T), id, url);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Creates a reference to the given asset that matches the given reference type.
        /// </summary>
        /// <param name="asset">The target asset of the reference to create.</param>
        /// <param name="referenceType">The type of reference to create.</param>
        /// <returns>A reference to the given asset if it's not null and <paramref name="referenceType"/> is a valid reference type, null otherwise.</returns>
        /// <remarks>A reference type is either an <see cref="AssetReference"/> or a content type registered in the <see cref="AssetRegistry"/>.</remarks>
        public static object CreateReference(AssetViewModel asset, Type referenceType)
        {
            if (asset != null)
            {
                if (UrlReferenceHelper.IsUrlReferenceType(referenceType))
                {
                    return(AttachedReferenceManager.CreateProxyObject(referenceType, asset.Id, asset.Url));
                }

                if (AssetRegistry.IsContentType(referenceType))
                {
                    var assetType   = asset.AssetItem.Asset.GetType();
                    var contentType = AssetRegistry.GetContentType(assetType);
                    return(referenceType.IsAssignableFrom(contentType) ? AttachedReferenceManager.CreateProxyObject(contentType, asset.Id, asset.Url) : null);
                }

                if (typeof(AssetReference).IsAssignableFrom(referenceType))
                {
                    return(new AssetReference(asset.AssetItem.Id, asset.AssetItem.Location));
                }
            }

            return(null);
        }
 /// <inheritdoc/>
 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 {
     return(UrlReferenceHelper.IsUrlReferenceType(TypeConverterHelper.GetDestinationType(context)));
 }
Beispiel #7
0
 public override bool CanVisit(Type type)
 {
     return(UrlReferenceHelper.IsUrlReferenceType(type));
 }