コード例 #1
0
        internal override bool Transform(string propName, string propData, ResolutionContext ctx, ConvertionManager cm, ExtensionResolution ex)
        {
            // convert to custom type (with an ObjectConverter registered in ConvertionManager).
            var objectConverter = cm.TryGet(typeof(string), ExtensionDataType);

            if (objectConverter == null)
            {
                return(false);
            }

            // if an property value is provided for the property name, try to convert it.
            object propValue;

            if (!objectConverter.TryConvert(propData, out propValue))
            {
                return(false);
            }

            var holder = new UInt16Holder(propValue);

            ex.Data.AddDataHolder(propName, holder);

            return(true);
        }
コード例 #2
0
        // Notes that this method will load the related assembly which defined the IExtensionBuilder implementation into memory.
        /// <summary>
        /// The required properties of extension builder (marked by ExtensionDataAttribute) must be provided by the extension data.
        /// </summary>
        /// <returns></returns>
        internal static bool ExtensionDataMatchesExtensionBuilder(this ExtensionResolution ex, IMessageDialog dialog,
                                                                  ResolutionContext ctx, ConvertionManager convertionManager)
        {
            var ebProperties = ex.ExtensionBuilder.Type.GetSettableRuntimeProperties();

            if (ebProperties == null)
            {
                return(true);
            }

            var data = ex.Data;

            foreach (var ebProp in ebProperties)
            {
                var    propName = ebProp.Name;
                string propInput;
                if (!data.TryGetString(propName, out propInput))
                {
                    var exPropAttrib = ebProp.GetCustomAttribute <ExtensionPropertyAttribute>(false, false);
                    if (exPropAttrib != null && exPropAttrib.Required)
                    {
                        dialog.AddError("a required property is missing!");
                        return(false);
                    }
                    continue;
                }

                #region specific types
                if (ebProp.PropertyType == typeof(string))
                {
                    var holder = new StringHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }

                // convert to TypeId
                if (ebProp.PropertyType == typeof(TypeId))
                {
                    TypeResolution type;
                    // a type dependency is introduced here.
                    // should it be added to the current addin's reference set?
                    if (!ctx.TryGetAddinType(ex.DeclaringAddin, propInput, out type))
                    {
                        dialog.AddError("");
                        return(false);
                    }
                    var holder = new LazyTypeIdHolder(type);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                #endregion

                // convert to custom type (with an ObjectConverter registered in ConvertionManager).
                var objectConverter = convertionManager.TryGet(typeof(string), ebProp.PropertyType);
                if (objectConverter == null)
                {
                    dialog.AddError("No converter is registered for !");
                    return(false);
                }

                // if an property value is provided for the property name, try to convert it.
                object propValue;
                if (!objectConverter.TryConvert(propInput, out propValue))
                {
                    dialog.AddError("The string [] can not be converted to !");
                    return(false);
                }

                #region common types
                if (ebProp.PropertyType == typeof(Int32))
                {
                    var holder = new Int32Holder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Boolean))
                {
                    var holder = new BooleanHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Version))
                {
                    var holder = new VersionHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(DateTime))
                {
                    var holder = new DateTimeHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Guid))
                {
                    var holder = new GuidHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(TimeSpan))
                {
                    var holder = new TimeSpanHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Int64))
                {
                    var holder = new Int64Holder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(UInt64))
                {
                    var holder = new UInt64Holder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(UInt32))
                {
                    var holder = new UInt32Holder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Int16))
                {
                    var holder = new Int16Holder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(UInt16))
                {
                    var holder = new UInt16Holder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Byte))
                {
                    var holder = new ByteHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(SByte))
                {
                    var holder = new SByteHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Char))
                {
                    var holder = new CharHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Decimal))
                {
                    var holder = new DecimalHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Double))
                {
                    var holder = new DoubleHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                if (ebProp.PropertyType == typeof(Single))
                {
                    var holder = new SingleHolder(propInput);
                    data.AddSerializableHolder(propName, holder);
                    continue;
                }
                #endregion
            }

            return(true);
        }