简体   繁体   English

ReadAsAsync - 错误:“Type是一个接口或抽象类,无法实例化。”

[英]ReadAsAsync - Error : “Type is an interface or abstract class and cannot be instantiated.”

I have the below code that gets an object from the web api service. 我有以下代码从web api服务获取对象。

At the following line of code 在以下代码行

response.Content.ReadAsAsync<CMLandingPage>().Result;

I get the following exception : 我得到以下异常:

InnerException = {"Could not create an instance of type MLSReports.Models.IMetaData. Type is an interface or abstract class and cannot be instantiated. Path 'BaBrInfo.Series[0].name', line 1, position 262."}

Any pointers are much appreciated. 任何指针都非常感谢。

 CMLandingPage lpInfo = new CMLandingPage();

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    // Add an Accept header for JSON format
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    response = client.PostAsJsonAsync(LPChartinfoapiURL, criteria.Mlsnums).Result;
                }
                // Throw exception if not a success code.
                response.EnsureSuccessStatusCode();
                // Parse the response body.
                lpInfo = response.Content.ReadAsAsync<CMLandingPage>().Result;
                }

CMLandingPage : CMLandingPage:

namespace MLSReports.Models
{
    public class CMLandingPage
    {
        public CMLandingPage()  {  }

        public CMColumn BaBrInfo { get; set; }

     }
  public class CMColumnItem<T> : IMetaData
    {
        #region Constructors and Methods
        public CMColumnItem()   {   }
        #endregion

        #region Properties and Fields
        public string name { get; set; }
        public List<T> data { get; set; }
        public string color { get; set; }
        #endregion

    }

    public class CMColumn
    {
        #region Constructor and Method
        public CMColumn()
        {
           Series = new List<IMetaData>();
        }
        #endregion

        #region Properties and Fields
        public string ChartType { get; set; }
        public string ChartTitle { get; set; }
        public List<IMetaData> Series { get; set; }
        #endregion    
    }
}

Your CmColumn class has this property on it: 您的CmColumn类上有此属性:

    public List<IMetaData> Series { get; set; }

The WebApi controller is evidently trying to construct an object based on the values of the parameters you're sending it. WebApi控制器显然试图根据您发送它的参数值构造一个对象。 When it gets to the value with the name "BaBrInfo.Series[0].name", it knows it should create a new IMetaData object so that it can set its name and add it to the Series property, but IMetaData is just an interface: it doesn't know what type of object to construct. 当它到达名为“BaBrInfo.Series [0] .name”的值时,它知道它应该创建一个新的IMetaData对象,以便它可以设置它的name并将其添加到Series属性,但IMetaData只是一个接口:它不知道要构造什么类型的对象。

Try changing IMetaData to some concrete type that implements that interface. 尝试将IMetaData更改为实现该接口的某种具体类型。

You need to get the serializer to include the type in the json payload and the deserializer to use the included type: 您需要让序列化程序在json有效负载和反序列化器中包含类型以使用包含的类型:

//Serialization
var config = new HttpConfiguration();
config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
return Request.CreateResponse(HttpStatusCode.OK, dto, config);

//Deserialization
var formatter = new JsonMediaTypeFormatter
{
    SerializerSettings = { TypeNameHandling = TypeNameHandling.Auto }
};
response.Content.ReadAsAsync<CMLandingPage>(new [] { formatter }).Result;

POLYMORPHIC SERIALIZATION USING NEWTON JSON.NET IN HTTPCONTENT 在HTTPCONTENT中使用NEWTON JSON.NET的多态串行化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型是接口或抽象类,无法实例化。 c#发送一个包含抽象对象列表的对象 - Type is an interface or abstract class and cannot be instantiated. c# send a object with contain a list of abstract object 类型是接口或抽象类,不能被实例化 - Type is an interface or abstract class and cannot be instantiated 无法创建类型的实例。 类型是接口或抽象类,不能被实例化 - Could not create an instance of type . Type is an interface or abstract class and cannot be instantiated C#JSON反序列化:Type是接口或抽象类,无法实例化 - C# JSON Deserialization : Type is an interface or abstract class and cannot be instantiated 为什么不能实例化抽象类和接口? - Why abstract class and interface cannot be instantiated? 无法创建类型 X 的实例。类型是接口或抽象类,无法实例化 - Could not create an instance of type X. Type is an interface or abstract class and cannot be instantiated 无法使用类型创建抽象类或接口接口的实例 - Cannot create an instance of the abstract class or interface interface with type 是一个抽象类的一种接口? - is an abstract class a type of interface? “无法创建抽象类或接口的实例” C#错误消息 - “Cannot create an instance of the abstract class or interface” C# error message 无法创建抽象类或接口的实例,但它不是抽象类,也不是接口? - Cannot create an instance of the abstract class or interface, but it is not an abstract class and it's not an interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM