同じ名前空間内に、2つのクラスを作成し、各々のクラス内に同じ名称のクラスをネストして作成し、
いずれのクラスにも[Serialize]属性をつけ、そのインスタンスを取得するためのWebMethodを作成して、Web参照の更新を行った ところエラーが発生。
同一の名前空間内に同じクラスを定義できないというエラーだったため、名前空間を指定するための下のような属性を追加した。
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/testclass1")]
その後Web参照の更新を行ったところエラーは発生しなかったが、Reference.csに定義された
ネストしたクラスのクラス名が、「クラス名1」のように後に1が追加されていた。
しかもネストしてほしいところネストが解除されていた。
入れ子のクラスを入れ子のままReference.csに記述されるようにするにはどうしたら良いでしょうか。
(1)クラスの定義とWebメソッド
namespace WebService1
{
/// <summary>
/// Service1 の概要の説明です
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public testclass1 GetTest1()
{
testclass1 t = new testclass1();
return t;
}
[WebMethod]
public testclass2 GetTest2()
{
testclass2 t = new testclass2();
return t;
}
}
[Serializable()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/testclass1")]
public class testclass1
{
public testclassSub[] tstsub;
[Serializable()]
public class testclassSub{}
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/testclass2")]
[Serializable()]
public class testclass2
{
public testclassSub[] tstsub;
[Serializable()]
public class testclassSub{}
}
}
(2)作成されたReference.cs 抜粋
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4016")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/testclass1")]
public partial class testclassSub {
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4016")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName="testclassSub", Namespace="http://tempuri.org/testclass2")]
public partial class testclassSub1 {
}