【问题标题】:Rest exception: Unable to find a constructor to use for type "myclass". A class should either have a default constructor休息异常:找不到用于类型“myclass”的构造函数。一个类应该有一个默认构造函数
【发布时间】:2017-07-10 10:14:32
【问题描述】:

我知道以前有人问过这个问题,但我找不到解决这个问题的答案。

我正在向返回 json 的 Web 服务发出请求,然后我使用 json.net 将该 json 作为对象保存在列表中。

List<myclass> result;
var request = new RestRequest(url, Method.POST);
//Set the parameters of the request
//[...]
IRestResponse response = client.Execute(request)
Console.WriteLine(response.Content);
//response.Content = [{"nomPrecio":"string","nomPrecioEN":"string","IDrangoPrecio":0,"IDPoblacionMv":0,"NumOfertas":0,"NumOVotaciones":0,"Imagen":"anUrl"}]
//Everything works fine until here, and I can see the json is being received OK, but then...
result = JsonConvert.DeserializeObject<List<myclass>>(response.Content);

然后控制台显示此消息:

Rest 异常:找不到用于类型 mynamespace.myclass 的构造函数。一个类应该有一个默认构造函数、一个带参数的构造函数或一个标有 JsonConstructor 属性的构造函数。路径“[0].nomPrecio”,第 1 行,位置 14。

namespace mynamespace
{
    public class myclass
    {
        public myclass()
        {
        }
        public myclass(string nomPrecio, string nomPrecioEN, int IDrangoPrecio, int IDPoblacionMv, int NumOfertas, int NumOVotaciones, string Imagen)
        {
            this.nomPrecio = nomPrecio;
            this.nomPrecioEN = nomPrecioEN;
            this.IDrangoPrecio = IDrangoPrecio;
            this.IDPoblacionMv = IDPoblacionMv;
            this.NumOfertas = NumOfertas;
            this.NumOVotaciones = NumOVotaciones;
            this.Imagen = Imagen;
        }
        public string nomPrecio { get; set; }
        public string nomPrecioEN { get; set; }
        public int IDrangoPrecio { get; set; }
        public int IDPoblacionMv { get; set; }
        public int NumOfertas { get; set; }
        public int NumOVotaciones { get; set; }
        public string Imagen { get; set; }
    }
}

更奇怪的是,我对应用程序中的其他类做了同样的事情,没有人返回这个错误,它们都有效。

我尝试了很多诸如“json2csharp”之类的东西,但没有任何效果。

关于我可能做错了什么的任何提示?谢谢

【问题讨论】:

    标签: c# xamarin constructor xamarin.ios json.net


    【解决方案1】:

    一些链接器问题mb?尝试为您的班级添加

    [Preserve(AllMembers = true)] 
    

    当链接器设置为“Sdk 和用户程序集”时会发生这种情况

    【讨论】:

    • 成功了!多么简单的解决方案!我在这上面浪费了很多时间...谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多