早上好,先生,
我试图弄清为什么我的客户为我的自定义类生成不同的名称空间(好的,问题不是为什么,但是如果我可以以某种方式更改此行为(。
一般示例在这里:在我的后端,我有两个不同的Web服务
- Menu.asmx
- category.asmx
类别的类声明wbservices似乎是这样的:
using System.Collections.Generic;
using System.Web.Services;
namespace backend.webservices
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Category : WebService
{
[WebMethod]
public List<CategoryType> GetFeaturedCategories(int idRegion, int idLanguage)
{
return CategoryService.GetFeaturedCategories(idRegion, idLanguage);
}
[WebMethod]
public CategoryType GetCategory(int idCategory, int idLanguage, int idRole, int idUser, int idRegion, bool generateBreadCrumb)
{
return CategoryService.GetCategory(idCategory, idLanguage, idRole, idUser, idRegion, generateBreadCrumb);
}
}
}
两个Web服务都有返回自定义类的方法:categoryType.cs
,我的类别类型闻起来像这样:
using System.Collections.Generic;
[Table("CATEGORY")]
public class CategoryType
{
[Column("ID_CATEGORY")]
public int? idCategory { get; set; }
[Column("ID_REGION")]
public int? idRegion { get; set; }
[Column("IN_STATUS")]
public int? inStatus { get; set; }
[Column("IN_MENU_SUPERIOR")]
public int? inMenuSuperior { get; set; }
[Column("NR_ORDER")]
public int? nrOrder { get; set; }
[Column("IN_CATALOG")]
public int? inCatalog { get; set; }
[Column("IN_FEATURED")]
public int? inFeatured { get; set; }
[Column("TX_MONGO_ID")]
public string txMongoId { get; set; }
//EXTERNAL
[Column("NM_CATEGORY")]
public string nmCategory { get; set; }
//NON ENTITY
public List<CategoryType> subCategories = new List<CategoryType>();
public List<BreadCrumbType> breadCrumb;
}
"问题"是,当我将这些Web服务导入前端(服务参考(时,它会在客户端中复制我的自定义类,因此,在代码中,我有两个类别类别typeytype.cs:
- wsmenu.categorytype
- wscategory.categorytype
令人讨厌的是它不能从一种类型转换为另一种类型:
WSMenu.CategoryType myExampleCategory = WSCategory.GetMyExampleCategory(idCategory);
casvey,这只是一个示例,如果A有单个代码行,这不是一个真正的问题。当项目开始增长时,问题就到了,在这种情况下,我将有很多从很多地方开始的论点。
提前感谢,祝您有美好的一天!而且请忽略一些晶状错误,我正在努力学习英语。
编辑:
所以,我发现我可以"转换"我的对象,也可以使用反射(new Object.properties = oher object.properties(编写转换器,但是我不想这样做,如果有一个不这样做的方法,我会很高兴
2个对象,完全相同(名称空间除外(C#
好吧,因为我的类具有自定义属性,所以我不能简单地创建一个反射映射器,在这种情况下,我必须为我要转换的每个自定义类创建自己的"映射器":
using MenuCategoryType = frontend.WSMenu.CategoryType;
using MenuBreadCrumbType = frontend.WSMenu.BreadCrumbType;
using CategoryCategoryType = frontend.WSCategory.CategoryType;
using CategoryBreadCrumbType = frontend.WSCategory.BreadCrumbType;
/// <summary>
/// converte objetos CategoryType de namespaces diferentes
/// </summary>
public class CategoryMapper
{
public static MenuCategoryType Convert(CategoryCategoryType origin)
{
if (origin == null)
return null;
return new MenuCategoryType()
{
idCategory = origin.idCategory,
idRegion = origin.idRegion,
inCatalog = origin.inCatalog,
inFeatured = origin.inFeatured,
inMenuSuperior = origin.inMenuSuperior,
inStatus = origin.inStatus,
nmCategory = origin.nmCategory,
nrOrder = origin.nrOrder,
txMongoId = origin.txMongoId,
breadCrumb = Convert(origin.breadCrumb),
subCategories = Convert(origin.subCategories)
};
}
public static CategoryCategoryType Convert(MenuCategoryType origin)
{
if (origin == null)
return null;
return new CategoryCategoryType() {
idCategory = origin.idCategory,
idRegion = origin.idRegion,
inCatalog = origin.inCatalog,
inFeatured = origin.inFeatured,
inMenuSuperior = origin.inMenuSuperior,
inStatus = origin.inStatus,
nmCategory = origin.nmCategory,
nrOrder = origin.nrOrder,
txMongoId = origin.txMongoId,
breadCrumb = Convert(origin.breadCrumb),
subCategories = Convert(origin.subCategories)
};
}
public static MenuBreadCrumbType[] Convert(CategoryBreadCrumbType[] origin)
{
if (origin == null)
return null;
MenuBreadCrumbType[] target = new MenuBreadCrumbType[origin.Length];
for(int i=0; i < origin.Length; i++)
{
target[i] = new MenuBreadCrumbType() {
idCategory = origin[i].idCategory,
nmCategory = origin[i].nmCategory
};
}
return target;
}
public static CategoryBreadCrumbType[] Convert(MenuBreadCrumbType[] origin)
{
if (origin == null)
return null;
CategoryBreadCrumbType[] target = new CategoryBreadCrumbType[origin.Length];
for (int i = 0; i < origin.Length; i++)
{
target[i] = new CategoryBreadCrumbType()
{
idCategory = origin[i].idCategory,
nmCategory = origin[i].nmCategory
};
}
return target;
}
public static MenuCategoryType[] Convert(CategoryCategoryType[] origin)
{
if (origin == null)
return null;
MenuCategoryType[] target = new MenuCategoryType[origin.Length];
for(int i = 0; i < origin.Length; i++)
{
target[i] = Convert(origin[i]);
}
return target;
}
public static CategoryCategoryType[] Convert(MenuCategoryType[] origin)
{
if (origin == null)
return null;
CategoryCategoryType[] target = new CategoryCategoryType[origin.Length];
for (int i = 0; i < origin.Length; i++)
{
target[i] = Convert(origin[i]);
}
return target;
}
}
编辑
我没有将自己的答案标记为正确的答案,只是因为这是解决问题的解决方法。