【问题标题】:System.MissingMethodException when using propertyGrid使用 propertyGrid 时出现 System.MissingMethodException
【发布时间】:2017-11-19 18:25:05
【问题描述】:

我有课

class PartitionTemplate
{
    public PartitionTemplate()
    {
        keepLabels = new List<string>();
        partitions = new List<partition>();
    }
    [JsonProperty("keepLabels")]
    public List<String> keepLabels { get; set; }
    [JsonProperty("slot")]
    public int slot { get; set; }
    ....
}

我的目标是用 propertyGrid 编辑它,使用以下代码:

    PartitionTemplate partiTemplate;
    //fi is FileInfo with the class as json using 
    //Newtonsoft.Json.JsonConvert.DeserializeObject<PartitionTemplate>(File.ReadAllText(partitionfile.FullName));
    PartitionTemplate.ReadOrCreatePartitonConfigurationFile(out partiTemplate, fi);
    propertyGrid1.SelectedObject = partiTemplate;

我的问题是: 当我尝试将add 元素转换为keepLabels 时,出现以下错误:

Exception thrown: 'System.MissingMethodException' in mscorlib.dll

Additional information: Constructor on type 'System.String' not found. 

如何解决?

【问题讨论】:

    标签: c# winforms json.net propertygrid


    【解决方案1】:

    发生这种情况是因为当您单击Collection Editor(属性网格的标准编辑器)中的“添加”按钮时,它会使用假定的公共无参数构造函数创建一个新项目,该构造函数在 System.String (你不能这样做var s = new String();)。

    如果您想保持 keepLabels 属性不变,您可以做的是创建一个自定义编辑器,如下所示:

    // decorate the property with this custom attribute  
    [Editor(typeof(StringListEditor), typeof(UITypeEditor))]
    public List<String> keepLabels { get; set; }
    
    ....
    
    // this is the code of a custom editor class
    // note CollectionEditor needs a reference to System.Design.dll
    public class StringListEditor : CollectionEditor
    {
        public StringListEditor(Type type)
            : base(type)
        {
        }
    
        // you can override the create instance and return whatever you like
        protected override object CreateInstance(Type itemType)
        {
            if (itemType == typeof(string))
                return string.Empty; // or anything else
    
            return base.CreateInstance(itemType);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多