【问题标题】:Getting Error "You must implement a default accessor on System.Array"出现错误“您必须在 System.Array 上实现默认访问器”
【发布时间】:2018-04-05 23:40:55
【问题描述】:

我有以下代码和网络方法:

public class RaumklassenHelper
{
   internal static Array Raumklasse()
   {
       List<object> raumKlassenObject = new List<object>();

       using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
       using (SqlCommand cmd = new SqlCommand(@"SELECT BEZEICHNUNG, BEZEICHNUNG_EN FROM RAUMKLASSE", con))
       {
           con.Open();
           using (SqlDataReader rdr = cmd.ExecuteReader())
           {
               while (rdr.Read())
               {
                   if (rdr["BEZEICHNUNG"] != DBNull.Value && rdr["BEZEICHNUNG_EN"] != DBNull.Value)
                   {
                       raumKlassenObject.Add(new
                           {
                               Name = rdr["BEZEICHNUNG"].ToString(),
                               Name_En = rdr["BEZEICHNUNG_EN"].ToString()
                           });
                   }
               }
           }
       }
       return raumKlassenObject.ToArray();
   }
}



 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
   [WebMethod]
   public Array Raumklasse()
   {
       return RaumklassenHelper.Raumklasse();
   }

如果我尝试调用方法,则会收到错误消息:

这是第一次发生错误。

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: &lt;&gt;f__AnonymousType0`2[System.String,System.String] cannot be serialized because it does not have a parameterless constructor.
   at System.Xml.Serialization.TypeDesc.CheckSupported()
   at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
   at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type)
   at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_ArrayOfAnyType(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()

【问题讨论】:

    标签: c# exception-handling ado.net anonymous-types


    【解决方案1】:

    尝试使用 object[] 作为返回类型而不是 Array


    关于其他问题:

        public class RaumklassenHelper
    {
    
            public class RAUMKLASSE
            {
                public string Name { get; set; }
                public string Name_En { get; set; }
            }
    
    
            internal static List<RAUMKLASSE> Raumklasse()
       {
           List<RAUMKLASSE> raumKlassenObject = new List<RAUMKLASSE>();
    
           using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
           using (SqlCommand cmd = new SqlCommand(@"SELECT BEZEICHNUNG, BEZEICHNUNG_EN FROM RAUMKLASSE", con))
           {
               con.Open();
               using (SqlDataReader rdr = cmd.ExecuteReader())
               {
                   while (rdr.Read())
                   {
                       if (rdr["BEZEICHNUNG"] != DBNull.Value && rdr["BEZEICHNUNG_EN"] != DBNull.Value)
                       {
                           raumKlassenObject.Add(new RAUMKLASSE()
                               {
                                   Name = rdr["BEZEICHNUNG"].ToString(),
                                   Name_En = rdr["BEZEICHNUNG_EN"].ToString()
                               });
                       }
                   }
               }
           }
           return raumKlassenObject;
       }
    }
    
    
    
     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
       [WebMethod]
       public List<RAUMKLASSE> Raumklasse()
       {
           return RaumklassenHelper.Raumklasse();
       }
    

    【讨论】:

    • 谢谢,解决了我的问题! :)
    • 嘿,艾哈迈德,你能再帮我一次吗?现在,如果我试图调用我的 Web 方法,我会得到一个 System.InvalidOperationException。我用错误更新了我的问题
    • 你为什么要创建 AnonymousType?尝试创建一个具有公共空构造函数的新类,并在其中添加 2 个属性(Name、Name_EN)并使您的 web 方法返回它的 IList
    • 我没有创建 AnonymousType。这个错误之前没有发生,我什么也没做。你能举个例子,你的意思是什么?
    猜你喜欢
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多