TypeDescriptor.GetConverter()不返回我的转换器 [英] TypeDescriptor.GetConverter() doesnt return my converter

查看:298
本文介绍了TypeDescriptor.GetConverter()不返回我的转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义类型转换器的简单类型,该类型转换器在运行时进行编译和加载. TypeDescriptor.GetConverter()找不到正确的转换器.这是一个独立的示例

I have a simple type with a custom type converter that is compiled and loaded at runtime. TypeDescriptor.GetConverter() doesn't find the right converter though. Here is a stand-alone example

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

public class Program
{
    private static string src =
@"
using System;
using System.ComponentModel;
namespace LoadMe
{
    [TypeConverter(typeof(FooConverter))]
    public class Foo
    {
    }
    public class FooConverter : TypeConverter
    {
        // stuff
    }
}
";
    public static void Main()
    {
        var codeProvider        = new CSharpCodeProvider(new     Dictionary<string, string>{{ "CompilerVersion", "v4.0" }});
        var compileParameters   = new CompilerParameters(new[] { "System.dll"     }) { GenerateInMemory = true };
        var compilerResults     = codeProvider.CompileAssemblyFromSource(compileParameters, src);

        if (compilerResults.Errors.Count == 0)
        {
            var fooType = compilerResults.CompiledAssembly.GetType("LoadMe.Foo");
            Console.WriteLine(fooType.FullName + "::" + fooType.Assembly.FullName);
            Console.WriteLine("Type converter type = '" + TypeDescriptor.GetConverter(fooType).GetType().FullName + "'");
        }
        else 
        {
            foreach (var err in compilerResults.Errors)
                Console.WriteLine(err);
        }
    }
}

以下是示例的输出(在vs中编译)

Here is the output of the example (compiled in vs)

LoadMe.Foo::q5sszdls, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Type converter type = 'System.ComponentModel.TypeConverter'

我应该自己挖掘属性并创建实例,还是在这里遗漏其他内容?

Should I just dig out the attribute and create the instance myself, or am I missing something else here?

也很奇怪!当在LINQPad中作为"C#程序"运行时,它实际上可以工作!这是输出. LINQPad就绪源

ALSO odd! When ran in LINQPad as a 'C# program', it actually works! here is the output. LINQPad ready source

LoadMe.Foo::oqmid5in, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Type converter type = 'LoadMe.FooConverter'

您知道LINQPad实际上有什么不同,以找到合适的转换器吗?

Any idea what LINQPad actually does differently to have it find the right converter?

推荐答案

.NET使用反射来加载指定的类型转换器.当您将Type实例传递给TypeConverterAttribute时,您的TypeConverterAttribute将包含程序集限定的类型名称.无法加载该程序集限定的类型名称,因为找不到该程序集. (是的,即使该程序集已经加载.)

.NET uses reflection to load the specified type converter. When you pass a Type instance to TypeConverterAttribute, your TypeConverterAttribute will contain an assembly-qualified type name. Loading that assembly-qualified type name does not work, because the assembly cannot be found. (Yes, even though that assembly is already loaded.)

您可以使用构造函数重载,该重载采用字符串来指定类型名称,而无需对定义的程序集进行任何引用,在这种情况下,它将在与您请求其转换器的类型相同的程序集中进行查找:

You can use the constructor overload that takes a string to specify the type name without any references to the defining assembly, in which case it will be looked up in the same assembly as the type whose converter you're requesting:

[TypeConverter("LoadMe.FooConverter")]
public class Foo
{
}

这篇关于TypeDescriptor.GetConverter()不返回我的转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆