public void ConvertTo()
        {
            ReferenceConverter converter     = new ReferenceConverter(typeof(ITestInterface));
            string             referenceName = "reference name";

            Assert.AreEqual("(none)", (string)converter.ConvertTo(null, null, null, typeof(string)), "#1");

            TestComponent component = new TestComponent();

            // no context
            Assert.AreEqual(String.Empty, (string)converter.ConvertTo(null, null, component, typeof(string)), "#2");

            // context with IReferenceService
            TestReferenceService referenceService = new TestReferenceService();

            referenceService.AddReference(referenceName, component);
            TestTypeDescriptorContext context = new TestTypeDescriptorContext(referenceService);

            Assert.AreEqual(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string)), "#3");

            // context with Component without IReferenceService
            Container container = new Container();

            container.Add(component, referenceName);
            context           = new TestTypeDescriptorContext();
            context.Container = container;
            Assert.AreEqual(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string)), "#4");
        }
        public void ConvertTo()
        {
            RemoteInvoke(() =>
            {
                CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;

                ReferenceConverter remoteConverter = new ReferenceConverter(typeof(ITestInterface));
                Assert.Equal("(none)", (string)remoteConverter.ConvertTo(null, null, null, typeof(string)));
            }).Dispose();

            ReferenceConverter converter     = new ReferenceConverter(typeof(ITestInterface));
            string             referenceName = "reference name";
            TestComponent      component     = new TestComponent();

            // no context
            Assert.Equal(String.Empty, (string)converter.ConvertTo(null, null, component, typeof(string)));

            // context with IReferenceService
            TestReferenceService referenceService = new TestReferenceService();

            referenceService.AddReference(referenceName, component);
            TestTypeDescriptorContext context = new TestTypeDescriptorContext(referenceService);

            Assert.Equal(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string)));

            // context with Component without IReferenceService
            Container container = new Container();

            container.Add(component, referenceName);
            context           = new TestTypeDescriptorContext();
            context.Container = container;
            Assert.Equal(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string)));
        }
        /// <summary>
        /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be
        /// used as generic method and casting is not required.
        /// <example>
        /// typeconverter.ConvertTo&lt;int&gt;(value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this ReferenceConverter typeconverter, Object value)
        {
            if (typeconverter == null)
            {
                throw new ArgumentNullException("typeconverter");
            }

            return((T)typeconverter.ConvertTo(value, typeof(T)));
        }
        /// <summary>
        /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be
        /// used as generic method and casting is not required.
        /// <example>
        /// referenceconverter.ConvertTo&lt;int&gt;(context, culture, value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this ReferenceConverter referenceconverter, ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value)
        {
            if (referenceconverter == null)
            {
                throw new ArgumentNullException("referenceconverter");
            }

            return((T)referenceconverter.ConvertTo(context, culture, value, typeof(T)));
        }
Exemplo n.º 5
0
        public void ConvertTo()
        {
            ReferenceConverter converter = new ReferenceConverter(typeof(ITestInterface));
            string referenceName = "reference name";

            Assert.Equal("(none)", (string)converter.ConvertTo(null, null, null, typeof(string)));

            TestComponent component = new TestComponent();

            // no context
            Assert.Equal(String.Empty, (string)converter.ConvertTo(null, null, component, typeof(string)));

            // context with IReferenceService
            TestReferenceService referenceService = new TestReferenceService();
            referenceService.AddReference(referenceName, component);
            TestTypeDescriptorContext context = new TestTypeDescriptorContext(referenceService);
            Assert.Equal(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string)));

            // context with Component without IReferenceService
            Container container = new Container();
            container.Add(component, referenceName);
            context = new TestTypeDescriptorContext();
            context.Container = container;
            Assert.Equal(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string)));
        }