Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TypeDescriptor.GetConverter(typeof(string))无法转换为我的自定义类型_C#_Type Conversion_Typeconverter - Fatal编程技术网

C# TypeDescriptor.GetConverter(typeof(string))无法转换为我的自定义类型

C# TypeDescriptor.GetConverter(typeof(string))无法转换为我的自定义类型,c#,type-conversion,typeconverter,C#,Type Conversion,Typeconverter,我正在使用一些第三方代码,它使用类型转换器将对象“强制转换”为指定为泛型参数的类型 第三方代码获取字符串类型转换器,并期望通过该转换器进行所有转换,例如 var typeConverter = TypeDescriptor.GetConverter(typeof(string)); 我已经为它编写了一个自定义类型和类型转换器(并用TypeDescriptor属性注册了它),但它没有被第三方代码使用,调用typeConverter.CanConvertTo(MyCustomType) 直到今天,

我正在使用一些第三方代码,它使用类型转换器将对象“强制转换”为指定为泛型参数的类型

第三方代码获取字符串类型转换器,并期望通过该转换器进行所有转换,例如

var typeConverter = TypeDescriptor.GetConverter(typeof(string));
我已经为它编写了一个自定义类型和类型转换器(并用TypeDescriptor属性注册了它),但它没有被第三方代码使用,调用
typeConverter.CanConvertTo(MyCustomType)

直到今天,我只在抽象中遇到了类型转换器,我见过提到它们,但从未构建或使用过

有人知道我做错了什么吗

我的削减代码

using System;
using System.ComponentModel;

namespace IoNoddy
{
[TypeConverter(typeof(TypeConverterForMyCustomType))]
public class MyCustomType
{
    public Guid Guid { get; private set; }
    public MyCustomType(Guid guid)
    {
        Guid = guid;
    }
    public static MyCustomType Parse(string value)
    {
        return new MyCustomType(Guid.Parse(value));
    }
}

public class TypeConverterForMyCustomType
    : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
    }
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        string strValue;
        if ((strValue = value as string) != null)
            try
            {
                return MyCustomType.Parse(strValue);
            }
            catch (FormatException ex)
            {
                throw new FormatException(string.Format("ConvertInvalidPrimitive: Could not convert {0} to MyCustomType", value), ex);
            }
        return base.ConvertFrom(context, culture, value);
    }
}
}

static void Main(string[] args)
{
    // Analogous to what 3rd party code is doing: 
    var typeConverter = TypeDescriptor.GetConverter(typeof(string));
    // writes "Am I convertible? false"
    Console.WriteLine("Am I convertible? {0}",  typeConverter.CanConvertTo(typeof(MyCustomType))); 
}

您选中CanConvertTo以便添加到您的转换器:

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return (destinationType == typeof(MyCustomType)) || base.CanConvertTo(context, destinationType);
    }
在某些地方:

    public static void Register<T, TC>() where TC : TypeConverter
    {
        Attribute[] attr = new Attribute[1];
        TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
        attr[0] = vConv;
        TypeDescriptor.AddAttributes(typeof(T), attr);
    }   
公共静态无效寄存器(),其中TC:TypeConverter
{
属性[]attr=新属性[1];
TypeConverterAttribute vConv=新的TypeConverterAttribute(typeof(TC));
attr[0]=vConv;
AddAttributes(typeof(T),attr);
}   
以及主要:

Register<string, TypeConverterForMyCustomType>();            
var typeConverter = TypeDescriptor.GetConverter(typeof(string));
Register();
var typeConverter=TypeDescriptor.GetConverter(typeof(string));

您的示例将在此之后工作。

您选中CanConvertTo,以便添加到您的转换器:

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return (destinationType == typeof(MyCustomType)) || base.CanConvertTo(context, destinationType);
    }
在某些地方:

    public static void Register<T, TC>() where TC : TypeConverter
    {
        Attribute[] attr = new Attribute[1];
        TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
        attr[0] = vConv;
        TypeDescriptor.AddAttributes(typeof(T), attr);
    }   
公共静态无效寄存器(),其中TC:TypeConverter
{
属性[]attr=新属性[1];
TypeConverterAttribute vConv=新的TypeConverterAttribute(typeof(TC));
attr[0]=vConv;
AddAttributes(typeof(T),attr);
}   
以及主要:

Register<string, TypeConverterForMyCustomType>();            
var typeConverter = TypeDescriptor.GetConverter(typeof(string));
Register();
var typeConverter=TypeDescriptor.GetConverter(typeof(string));

您的示例将在这之后开始工作。

开始阅读的好地方希望它能帮助您注册转换器?@gabba:想详细说明一下吗?还是我应该坐在这里用拐杖打自己?@Binary Worrier我只是告诉你你做错了什么。试着了解第三方代码是如何了解您的类型以及与此类型相关的转换器的?@gabba:这是一根弯腰棍,所以?开始阅读的好地方希望它能帮上忙也许你应该注册你的转换器?@gabba:想详细说明一下吗?还是我应该坐在这里用拐杖打自己?@Binary Worrier我只是告诉你你做错了什么。试着了解第三方代码是如何了解您的类型以及与此类型相关的转换器的?@gabba:这是一根弯腰棒,所以?您是一个温柔(wo)的人,我为我昨天对您的任何恶意道歉。。。老实说,我真的希望疖子很快就好:)你是个温柔的人,我为昨天我对你许下的任何愿望向你道歉。。。老实说,我真的希望疖子很快就好:)