当前位置: 首页>>代码示例>>C#>>正文


C# IConverter.Convert方法代码示例

本文整理汇总了C#中IConverter.Convert方法的典型用法代码示例。如果您正苦于以下问题:C# IConverter.Convert方法的具体用法?C# IConverter.Convert怎么用?C# IConverter.Convert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IConverter的用法示例。


在下文中一共展示了IConverter.Convert方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestConversion

        public static void TestConversion(IConverter converter, string sourceFileName, FileTypes sourceFormat, FileTypes targetFormat, bool assertSuccess)
        {
            string sourceFile = Path.GetFullPath(sourceFileName);

            // Get a temporary target file
            TempFolder tempFolder = new TempFolder();
            string targetFile = tempFolder.getFilePath("target." + targetFormat);

            // Do the conversion
            bool converted = converter.Convert(sourceFile, (int)sourceFormat, targetFile, (int)targetFormat);

            if (assertSuccess)
            {
                // Check that converter returned true
                Assert.IsTrue(converted);
                // Check that converter created the target file and it's not empty
                Assert.AreNotEqual(0, new FileInfo(targetFile).Length);
            }
            else
            {
                // Check that converter returned false
                Assert.IsFalse(converted);
            }

            // Delete the temp folder created
            tempFolder.Release();
        }
开发者ID:sbxlmdsl,项目名称:MateCat-Win-Converter,代码行数:27,代码来源:ConversionTestHelper.cs

示例2: Convert

        /// <inheritdoc />
        public object Convert(object sourceValue, Type targetType, IConverter elementConverter, bool nullable)
        {
            var sourceArray = (Array)sourceValue;
            int length = sourceArray.Length;

            Type targetElementType = targetType.GetElementType();
            Array targetArray = Array.CreateInstance(targetElementType, length);

            for (int i = 0; i < length; i++)
                targetArray.SetValue(elementConverter.Convert(sourceArray.GetValue(i), targetElementType), i);

            return targetArray;
        }
开发者ID:dougrathbone,项目名称:mbunit-v3,代码行数:14,代码来源:ArrayToArrayConversionRule.cs

示例3: Add

        public static Section Add(this Section section, string contents, IConverter converter)
        {
            if (string.IsNullOrEmpty(contents))
            {
                throw new ArgumentNullException("contents");
            }
            if (converter == null)
            {
                throw new ArgumentNullException("converter");
            }

            var addAction = converter.Convert(contents);
            addAction(section);
            return section;
        }
开发者ID:JoshSchreuder,项目名称:MigraDoc.Extensions,代码行数:15,代码来源:SectionExtensions.cs

示例4: OnEntry

        public override void OnEntry(MethodExecutionArgs args)
        {
            _stopWatch = Stopwatch.StartNew();
            _converter = new JsonConverter();
            var stringBuilder = new StringBuilder().AppendFormat("{0} [", args.Method.Name);

            for (var index = 0; index < args.Arguments.Count; index++)
            {
                var arg = args.Arguments.GetArgument(index);
                stringBuilder.AppendFormat("({0}: {1}) ", args.Method.GetParameters()[index].ParameterType.Name,
                    _converter.Convert(arg));
            }
            MvcApplication.Log.Info(stringBuilder.Append(']').ToString());
            base.OnEntry(args);
        }
开发者ID:Stelmashenko-A,项目名称:MVC,代码行数:15,代码来源:LogAspect.cs

示例5: BindParameter

		private void BindParameter(ViewComponentParamAttribute paramAtt, PropertyInfo property, IConverter converter)
		{
			var compParamKey = string.IsNullOrEmpty(paramAtt.ParamName) ? property.Name : paramAtt.ParamName;

			var value = ComponentParams[compParamKey] ?? paramAtt.Default;

			if (value == null)
			{
				if (paramAtt.Required &&
				    (property.PropertyType.IsValueType || property.GetValue(this, null) == null))
				{
					throw new ViewComponentException(string.Format("The parameter '{0}' is required by " +
					                                               "the ViewComponent {1} but was not passed or had a null value",
					                                               compParamKey, GetType().Name));
				}
			}
			else
			{
				try
				{
					bool succeeded;

					var converted = converter.Convert(property.PropertyType, value.GetType(), value, out succeeded);

					if (succeeded)
					{
						property.SetValue(this, converted, null);
					}
					else
					{
						throw new Exception("Could not convert '" + value + "' to type " + property.PropertyType);
					}
				}
				catch (Exception ex)
				{
					throw new ViewComponentException(string.Format("Error trying to set value for parameter '{0}' " +
					                                               "on ViewComponent {1}: {2}", compParamKey, GetType().Name,
					                                               ex.Message), ex);
				}
			}
		}
开发者ID:rambo-returns,项目名称:MonoRail,代码行数:41,代码来源:ViewComponent.cs

示例6: AddHtml

    public Section AddHtml(ExCSS.Stylesheet sheet, string contents, IConverter converter)
    {
        if (string.IsNullOrEmpty(contents))
        {
            throw new ArgumentNullException("contents");
        }
        if (converter == null)
        {
            throw new ArgumentNullException("converter");
        }

        Action<Section> addAction = converter.Convert(sheet, contents);
        addAction(this);
        return this;
    }
开发者ID:jgshumate1,项目名称:Migradoc,代码行数:15,代码来源:Section.cs

示例7: OnSuccess

 public override void OnSuccess(MethodExecutionArgs args)
 {
     _converter = new JsonConverter();
     MvcApplication.Log.Info(_converter.Convert(args.ReturnValue) + " [Elapsed time: " + _stopWatch.ElapsedMilliseconds + ']');
 }
开发者ID:Stelmashenko-A,项目名称:MVC,代码行数:5,代码来源:LogAspect.cs

示例8: TestHighConcurrencyConversion

        public static void TestHighConcurrencyConversion(IConverter converter, string sourceFileName, FileTypes sourceFormat, FileTypes targetFormat, int concurrentConversions)
        {
            // Try to convert always the same file
            string sourceFile = Path.GetFullPath(sourceFileName);

            // Count the succeded conversion in this var
            int successes = 0;

            // Create the thread pool
            List<Thread> threads = new List<Thread>();
            for (int i = 0; i < concurrentConversions; i++)
            {
                threads.Add(new Thread(delegate () {
                    // This code will run many times in the same moment on many threads,
                    // so be very careful to not make two Office instances write the same
                    // file, or they will raise exceptions. The most problems I had 
                    // happened exactly because of this. Always make Office instance write
                    // to a clean filepath that points to a still non-existent file.
                    // Don't try anything else, 90% it will raise errors.

                    // The safest way to create a clean lonely filepath is using the 
                    // TempFolder class
                    TempFolder tempFolder = new TempFolder();
                    string targetFile = tempFolder.getFilePath("target." + targetFormat);

                    // Ok, let's do the real conversion
                    try
                    {
                        bool converted = converter.Convert(sourceFile, (int)sourceFormat, targetFile, (int)targetFormat);
                        if (converted) Interlocked.Increment(ref successes);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Conversion failed:\n" + e + "\n");
                    }
                    finally
                    {
                        // Delete the temp target folder
                        tempFolder.Release();
                    }
                }));
            }

            // Launch all the threads in the same time
            foreach (var thread in threads)
            {
                thread.Start();
            }

            // Wait for all threads completion
            foreach (var thread in threads)
            {
                thread.Join();
            }

            // Check test final result
            Assert.AreEqual(concurrentConversions, successes);
        }
开发者ID:sbxlmdsl,项目名称:MateCat-Win-Converter,代码行数:58,代码来源:ConversionTestHelper.cs

示例9: Convert

 /// <inheritdoc />
 public object Convert(object sourceValue, Type targetType, IConverter elementConverter, bool nullable)
 {
     return elementConverter.Convert(sourceValue.ToString(), targetType);
 }
开发者ID:dougrathbone,项目名称:mbunit-v3,代码行数:5,代码来源:ObjectToStringConversionRule.cs

示例10: HandlePasteAction

 private void HandlePasteAction(IConverter converter)
 {
     var data = Clipboard.GetDataObject();
     // If the data is text, then set the text of the
     // TextBox to the text in the Clipboard.
     if (data != null && data.GetDataPresent(DataFormats.Text))
     {
         var expenses = converter.Convert(data.GetData(DataFormats.Text).ToString());
         foreach (var expense in expenses)
         {
             _controller.AddExpense(expense);
         }
     }
 }
开发者ID:thcbin1110,项目名称:ExpenseProject,代码行数:14,代码来源:MainPageView.cs

示例11: ConvertAUrlToPdf

        /// <summary>
        /// Convert a url to a pdf
        /// </summary>
        /// <param name="ConverterToUse">converter to use. Use TuesConverter.PdfConverter</param>
        /// <param name="ProduceTableOfContentsOutline">Product the table of contents outline</param>
        /// <param name="PaperSize">Paper Size</param>
        /// <param name="WhatToRun">What to run. Either Html or a url</param>
        /// <param name="ModeToRun">What mode is the What To Run In.</param>
        /// <param name="HtmlToConvert">Html to convert</param>
        /// <param name="MarginTop">Margin Top</param>
        /// <param name="MarginRight">Margin Right</param>
        /// <param name="MarginBottom">Margin Bottom</param>
        /// <param name="MarginLeft">Margin Left</param>
        /// <param name="UsePrintMediaCssSelectors">When running do you want to run under the css 3 print media selectors</param>
        /// <returns>pdf file byte array</returns>
        public static byte[] ConvertAUrlToPdf(IConverter ConverterToUse,
                                              bool ProduceTableOfContentsOutline,
                                              System.Drawing.Printing.PaperKind PaperSize,
                                              IWhatToRunParameter WhatToRun,
                                              double? MarginTop,
                                              double? MarginRight,
                                              double? MarginBottom,
                                              double? MarginLeft,
                                              bool UsePrintMediaCssSelectors)
        {
            //let's build up the object settings
            var ObjectSettingsToUse = WhatToRun.ToObjectSettings();

            //add anything else now
            ObjectSettingsToUse.WebSettings = new WebSettings { PrintMediaType = UsePrintMediaCssSelectors };

            //go build the main settings to use
            var DocumentSettings = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ProduceOutline = ProduceTableOfContentsOutline,
                    PaperSize = PaperSize,
                    Margins =
                    {
                        Top = MarginTop,
                        Right = MarginRight,
                        Bottom = MarginBottom,
                        Left = MarginLeft,
                        Unit = Unit.Inches
                    }
                },
                Objects =
                {
                    ObjectSettingsToUse
                }
            };

            //go convert and return it
            return ConverterToUse.Convert(DocumentSettings);
        }
开发者ID:dibiancoj,项目名称:ToracLibrary,代码行数:56,代码来源:HtmlToPdfConverter.cs

示例12: Convert

 /// <inheritdoc />
 public object Convert(object sourceValue, Type targetType, IConverter elementConverter, bool nullable)
 {
     IXPathNavigable node = (IXPathNavigable)sourceValue;
     return elementConverter.Convert(node.CreateNavigator(), targetType);
 }
开发者ID:dougrathbone,项目名称:mbunit-v3,代码行数:6,代码来源:XPathNavigableToXPathNavigatorConversionRule.cs

示例13: Convert

 /// <inheritdoc />
 public object Convert(object sourceValue, Type targetType, IConverter elementConverter, bool nullable)
 {
     var doc = new XmlDocument();
     doc.LoadXml((string)sourceValue);
     return elementConverter.Convert(doc, targetType);
 }
开发者ID:dougrathbone,项目名称:mbunit-v3,代码行数:7,代码来源:StringToXmlDocumentConversionRule.cs

示例14: Convert

 /// <inheritdoc />
 public object Convert(object sourceValue, Type targetType, IConverter elementConverter, bool nullable)
 {
     XPathNavigator node = (XPathNavigator)sourceValue;
     return elementConverter.Convert(node.Value, targetType);
 }
开发者ID:dougrathbone,项目名称:mbunit-v3,代码行数:6,代码来源:XPathNavigatorToStringConversionRule.cs


注:本文中的IConverter.Convert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。