找不到String的构造函数

时间:2015-05-28 16:49:20

标签: c# arrays propertygrid

如果我在包含List<string[]>Dictionary<string,string[]>的属性网格中有一个对象(使用GenericDictionaryEditor),当我点击该属性旁边的详细信息并单击添加时,会弹出一条消息说没有找到构造函数(列表)或没有找到无参数构造函数(对于字典)。我真的不了解编辑或财产网格,任何帮助都会受到赞赏。

[DataMember(Name="FolderPaths")]
[ReadOnly(false)]
[Description("List of folder paths")]
[Editor(typeof(Wexman.Design.GenericDictionaryEditor<string, string[]>), typeof(UITypeEditor))]
[Wexman.Design.GenericDictionaryEditor(Title="Folder Paths")]
public Dictionary<string, string[]> FolderPaths { get; set; }

这个说没有为System.string []找到构造函数。

[DataMember(Name="FolderPaths")]
[ReadOnly(false)]
[Description("List of folder paths")]
public List<string[]> FolderPaths { get; set; }

1 个答案:

答案 0 :(得分:2)

一种解决方案是为string[]类型注册TypeDescriptionProvider。下面是示例代码(您需要在显示任何属性网格之前在程序开头注册):

...
TypeDescriptor.AddProvider(new StringArrayDescriptionProvider(), typeof(string[]));
...

以下是StringArrayDescriptionProvider代码:

public class StringArrayDescriptionProvider : TypeDescriptionProvider
{
    private static TypeDescriptionProvider _baseProvider;

    static StringArrayDescriptionProvider()
    {
       // get default metadata
        _baseProvider = TypeDescriptor.GetProvider(typeof(string[]));
    }

    public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
    {
        // this is were we define create the instance
        // NB: .NET could do this IMHO...
        return Array.CreateInstance(typeof(string), 0);
    }

    public override IDictionary GetCache(object instance)
    {
        return _baseProvider.GetCache(instance);
    }

    public override ICustomTypeDescriptor GetExtendedTypeDescriptor(object instance)
    {
        return _baseProvider.GetExtendedTypeDescriptor(instance);
    }

    public override string GetFullComponentName(object component)
    {
        return _baseProvider.GetFullComponentName(component);
    }

    public override Type GetReflectionType(Type objectType, object instance)
    {
        return _baseProvider.GetReflectionType(objectType, instance);
    }

    public override Type GetRuntimeType(Type reflectionType)
    {
        return _baseProvider.GetRuntimeType(reflectionType);
    }

    public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
    {
        return _baseProvider.GetTypeDescriptor(objectType, instance);
    }

    public override bool IsSupportedType(Type type)
    {
        return _baseProvider.IsSupportedType(type);
    }
}

它的外观如下: enter image description here