コード例 #1
0
 IControlConverter IConverterContext.Convert(object obj, XmlWriter writer, IConverterContext context, ConverterArguments args)
 {
     return HtmlFactory.Convert(obj, context ?? (this as IConverterContext), writer, args);
 }
コード例 #2
0
ファイル: HtmlFactory.cs プロジェクト: thomas13335/wpf2html5
        /// <summary>Creates a <see cref="ControlConverter"/> for a control.</summary>
        /// <param name="obj">The control to convert.</param>
        /// <param name="context">The context in which the conversion takes place.</param>
        /// <param name="writer">XML writer to receive the converted HTML.</param>
        /// <param name="args">Arguments for the conversion.</param>
        /// <returns></returns>
        public static IControlConverter Convert(object obj, IConverterContext context, XmlWriter writer, ConverterArguments args)
        {
            Type originaltype, type, convertertype = null;

            // ascend the derivation path until ...
            type = originaltype = obj.GetType();
            while (null != type)
            {
                // a previously used converter matches
                if (_typemap.TryGetValue(type, out convertertype))
                {
                    break;
                }

                // or a converter existings in the Factory namespace.
                if (null != (convertertype = LookupConverter(type)))
                {
                    break;
                }

                type = type.BaseType;
            }

            if (null == convertertype)
            {
                throw new Exception("unable to convert [" + obj.GetType().FullName + "].");
            }

            // using a baseclass converter?
            var baseclassconverter = type != originaltype;

            /*if(originaltype.GetCustomAttributes(false).OfType<GeneratorIgnoreAttribute>().Any())
            {
                baseclassconverter = false;
            }*/

            // TraceTarget.Trace("using [{0}] to convert [{1}] ...", convertertype.FullName, originaltype.FullName);

            // construct the converter object
            var constructor = convertertype.GetConstructor(new Type[0]);
            if (null == constructor)
            {
                throw new ConverterException(ErrorCode.InvalidConverterType, 
                    "type [" + convertertype.FullName + "] constructor unavailable.");
            }

            var converter = constructor.Invoke(new object[0]) as IControlConverter;
            if (null == converter)
            {
                throw new ConverterException(ErrorCode.InvalidConverterType, 
                    "type [" + convertertype.FullName + "] does not support converter interface.");
            }

            converter.Context = context;
            converter.IsSubClass = baseclassconverter;
            converter.SetControl(obj);
            converter.Convert(writer, args);

            if (converter.IsTypeRegistrable)
            {
                // add reference ...
                context.TriggerItemReference(originaltype.FullName);
            }

            return converter;
        }