0

I want to return List in my web method like below,

public List<Object> MyMethod(){

List<Object> list = new List<Object>();
myClass a = new myClass();
list.Add(a);

return list;

}

public class myClass{
public int StudetNumber {get; set;}
public string StudentName {get; set;}
public decimal average {get; set;}
}

But i get this error : System.InvalidOperationException : myClass is not expected. System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(string name, string ns, Object o , Boolean xsiType);

I don't get the error when it returns List of myClass. It is possible to return List of System.Object or Do I have to return List of myClass ?

Thanks for advance.

asyaben
  • 55
  • 2
  • 8

2 Answers2

0

you are missing the reserved word 'class' in your class definition you are missing a few ';'s

  public class myClass
    {
        public int StudetNumber { get; set; }
        public string StudentName { get; set; }
        public decimal average { get; set; }
    }
jazb
  • 5,498
  • 6
  • 37
  • 44
  • Hi, thanks for your answer but i wrote the question in mobile phone and i forgot the things you sad :) Normally in project myClass is the like you wrote above. – asyaben Oct 11 '18 at 07:56
0

It seems that I understand now your question. It seems that it is not compatible returning a generic type such as "Object" or List<Object> in a web method, because in a web method you'll have an XML definition (WSDL) and there is no way to define a generic type in that definition. So it seems that you need to always return a defined class, in your case "myClass".

Take a look to the following post where this topic was talked so it would be a little bit more clear for you:

How do I return a C# web service that has a generic property?

halfer
  • 19,824
  • 17
  • 99
  • 186
Brank Victoria
  • 1,447
  • 10
  • 17