其余例外:无法找到用于类型"myclass"的构造函数.一个类应该具有默认的构造函数 [英] Rest exception: Unable to find a constructor to use for type "myclass". A class should either have a default constructor

查看:371
本文介绍了其余例外:无法找到用于类型"myclass"的构造函数.一个类应该具有默认的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题曾经被问过,但是我找不到解决这个问题的答案.

I know this question has been asked before, but I can't find an answer to solve this problem.

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

I'm making a request to a web service which returns a json, and then I save that json as an object in a list using json.net.

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

然后控制台显示以下消息:

Then the console shows this message:

其他异常:无法找到用于类型mynamespace.myclass的构造函数.一个类应该具有一个默认的构造函数,一个带有参数的构造函数或一个标有JsonConstructor属性的构造函数.路径"[0] .nomPrecio",第1行,位置14.

Rest Exception: Unable to find a constructor to use for type mynamespace.myclass. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path '[0].nomPrecio', line 1, position 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; }
    }
}

更奇怪的是,我对应用程序中的其他类进行了相同的设置,没有人返回此错误,所有这些都起作用.

What's more weird is that I make the same for other classes in the app and no one returns this error, all of them works.

我尝试了很多类似"json2csharp"的方法,但是没有任何效果.

I tried a lot of things like "json2csharp" but nothing works.

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

Any tip about what could I be doing wrong? Thanks

推荐答案

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

Some linker problem mb? Try to add for your class

[Preserve(AllMembers = true)] 

将链接器设置为"SDK和用户程序集"时可能会发生

That can happen when linker is set to "Sdk and user assemblies"

这篇关于其余例外:无法找到用于类型"myclass"的构造函数.一个类应该具有默认的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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