MoCoolKaErp只有一个入口,接收参数为基类,按WCF协议必须声明所有的子类为knownType.最直接的办法就是遍历所有assembly,寻找所有的子类。
用linq遍历assembly寻找指定类生成web.config中序列化
linq语法的确精炼,一句话就行了
方法
public XElement GenConfig(String[] assemblys)
{
return new XElement("system.runtime.serialization", new XElement("dataContractSerializer",
new XElement("declaredTypes",
new XElement("add", new XAttribute("type", typeof(MKEntity).AssemblyQualifiedName),
(from ass in assemblys
let assembly = Assembly.Load(ass)
select from type in assembly.GetTypes()
where (type.IsClass && type.IsSubclassOf(typeof(MKEntity)))
select (new XElement("knownType", new XAttribute("type", type.AssemblyQualifiedName))))
))))
;
}
调用
[TestMethod()]
public void GenConfigTest()
{
MKGenConfig target = new MKGenConfig(); // TODO: Initialize to an appropriate value
string[] assemblys = new String[] { "MoCoolKaCoreErp", "MoCoolKaErp" };// TODO: Initialize to an appropriate value
XElement expected = null; // TODO: Initialize to an appropriate value
XElement actual;
actual = target.GenConfig(assemblys);
//Assert.AreEqual(expected, actual);
//Assert.Inconclusive("Verify the correctness of this test method.");
}
生成结果
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="MoCoolKaErp.Model.MKEntity, MoCoolKaErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<knownType type="MoCoolKaCoreErp.Model.Access.MKPurviewEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.UI.MKColumnEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.DataModel.MKFieldEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.DataModel.MKTableEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.UI.MKTabEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.UI.MKWindowEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Code.MKCodeClassEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Code.MKCodeEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Access.MKConfigEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Access.MKRoleEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Access.MKUserEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
用linq遍历assembly寻找指定类生成web.config中序列化
linq语法的确精炼,一句话就行了
方法
public XElement GenConfig(String[] assemblys)
{
return new XElement("system.runtime.serialization", new XElement("dataContractSerializer",
new XElement("declaredTypes",
new XElement("add", new XAttribute("type", typeof(MKEntity).AssemblyQualifiedName),
(from ass in assemblys
let assembly = Assembly.Load(ass)
select from type in assembly.GetTypes()
where (type.IsClass && type.IsSubclassOf(typeof(MKEntity)))
select (new XElement("knownType", new XAttribute("type", type.AssemblyQualifiedName))))
))))
;
}
调用
[TestMethod()]
public void GenConfigTest()
{
MKGenConfig target = new MKGenConfig(); // TODO: Initialize to an appropriate value
string[] assemblys = new String[] { "MoCoolKaCoreErp", "MoCoolKaErp" };// TODO: Initialize to an appropriate value
XElement expected = null; // TODO: Initialize to an appropriate value
XElement actual;
actual = target.GenConfig(assemblys);
//Assert.AreEqual(expected, actual);
//Assert.Inconclusive("Verify the correctness of this test method.");
}
生成结果
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="MoCoolKaErp.Model.MKEntity, MoCoolKaErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<knownType type="MoCoolKaCoreErp.Model.Access.MKPurviewEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.UI.MKColumnEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.DataModel.MKFieldEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.DataModel.MKTableEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.UI.MKTabEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.UI.MKWindowEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Code.MKCodeClassEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Code.MKCodeEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Access.MKConfigEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Access.MKRoleEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<knownType type="MoCoolKaCoreErp.Model.Access.MKUserEO, MoCoolKaCoreErp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>