找不到String的构造方法 [英] Constructor not found for String

查看:171
本文介绍了找不到String的构造方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在属性网格中有一个包含 List< string []> Dictionary< string,string []> (使用 GenericDictionaryEditor ),当我单击属性旁边的详细信息并单击添加时,弹出消息,提示找不到任何构造函数(用于列表)或找不到无参数构造函数(用于字典).我真的不了解编辑器或属性网格,将不胜感激.

If I have an object in a property grid containing a List<string[]> or Dictionary<string,string[]>(using GenericDictionaryEditor), when I click on the detail beside the property and click add, a message pops up saying that no constructor can be found (for the list) or no parameterless constructor found (for the dictionary). I don't understand editors or the property grid really, and any help would be appreciated.

[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 []的构造函数.

This one says constructor not found for System.string[].

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

推荐答案

一种解决方案是注册

One solution is to register a TypeDescriptionProvider for the string[] type. Here is the sample code (you need to register this at the beginning of your program, before displaying any property grid):

...
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);
    }
}

这是它的外观:

这篇关于找不到String的构造方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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