C# 使用C泛型方法并使其成为强类型
我有两种配置文件类型,它们是:C# 使用C泛型方法并使其成为强类型,c#,asp.net-mvc,generics,C#,Asp.net Mvc,Generics,我有两种配置文件类型,它们是: TeamPlayerPage.cs & TeamStaffMemberPage 在我的代码(这是一个Umbraco解决方案)中,我创建了两个方法,用于获取和建立配置文件集合,例如 GetPlayerProfiles(TeamPlayersLandingPage teamPlayersPage) GetStaffProfiles(TeamStaffLandingPage staffMembersPage) 在上述每个方法中,我创建了一个赞助商列表项,以将
TeamPlayerPage.cs & TeamStaffMemberPage
在我的代码(这是一个Umbraco解决方案)中,我创建了两个方法,用于获取和建立配置文件集合,例如
GetPlayerProfiles(TeamPlayersLandingPage teamPlayersPage)
GetStaffProfiles(TeamStaffLandingPage staffMembersPage)
在上述每个方法中,我创建了一个赞助商列表项,以将赞助商与配置文件相关联。下面的例子
private SponsorListItem GetPlayerSponsor(TeamPlayerPage teamPlayerPage)
{
if (teamPlayerPage.Sponsor == null)
return null;
var sponsorPage = teamPlayerPage.Sponsor as SponsorPage;
var sponsor = new SponsorListItem()
{
Heading = sponsorPage.Heading,
Url = sponsorPage.Url,
ListingImgUrl = sponsorPage.Image != null ? sponsorPage.GetCropUrl("image", "360x152(listing)") : Global.PlaceholderImage.GenericListingItem,
KeySponsor = sponsorPage.KeySponsor
};
return sponsor;
}
每种类型的发起人逻辑完全相同,因此我想创建一个通用方法,例如GetProfileSponsorT profilePage,而不是两个。请参见下文。目标是能够将aa TeamPlayerPage或TeamStaffMemberPage传递给通用方法,并将其强类型化,以便我可以访问其上的属性
private SponsorListItem GetPlayerSponsor(TeamPlayerPage teamPlayerPage)
private SponsorListItem GetStaffSponsor(TeamStaffMemberPage staffMembersPage)
我创建了以下内容,但我不确定如何根据传入的内容强类型化T profilePage参数(如果可能的话)
我做了一些搜索,但努力理解这个概念一点。有人能给我指一下正确的方向吗
多谢各位
Paul要做到这一点,您的两个类都需要实现一个公共接口,以便您可以将该方法约束到该接口。例如:
public interface ISponsor
{
SponsorPage Sponsor { get; }
}
然后让您的类实现该接口:
public class TeamPlayersLandingPage : ISponsor
{
}
现在,您可以约束您的泛型方法:
private SponsorListItem GetProfileSponsor<T>(T profilePage)
where T : ISponsor
{
var sponsor = profilePage.Sponsor;
}
要做到这一点,您的两个类都需要实现一个公共接口,以便您可以将该方法约束到该接口。例如:
public interface ISponsor
{
SponsorPage Sponsor { get; }
}
然后让您的类实现该接口:
public class TeamPlayersLandingPage : ISponsor
{
}
现在,您可以约束您的泛型方法:
private SponsorListItem GetProfileSponsor<T>(T profilePage)
where T : ISponsor
{
var sponsor = profilePage.Sponsor;
}
TeamStaffMemberPage是否也有类型为SponsorPage的赞助商属性?顺便问一下,为什么您需要在teamPlayerPage中作为赞助商。赞助商作为赞助商;?现在什么都不可能。您应该考虑将T限制为某种类型,这种类型可以获取赞助商isponsable I您有一个这样的接口,您不需要做任何其他事情,您也可以将其作为参数GetSponsorisponsablepage@Sweeper,我正在使用Umbraco和models builder。我在teamPlayerPage上有一个返回IPPublishedContent的内容选择器属性,因此在这一行代码中,我将其转换为modelsBuilder生成的一个SponsorPage,这样我就可以直接访问这些属性,而不是teamPlayerPage.赞助商.ValuesomePropValue.Thank you@PaulAdam,我会看一看。请容忍我,因为这个概念对我来说是相当新的。如果我得到There,是否会反馈我的解决方案TeamStaffMemberPage是否也有一个类型为SponsorPage的赞助商属性?顺便问一下,为什么您需要在teamPlayerPage中作为赞助商。赞助商作为赞助商;?现在什么都不可能。您应该考虑将T限制为某种类型,这种类型可以获取赞助商isponsable I您有一个这样的接口,您不需要做任何其他事情,您也可以将其作为参数GetSponsorisponsablepage@Sweeper,我正在使用Umbraco和models builder。我在teamPlayerPage上有一个返回IPPublishedContent的内容选择器属性,因此在这一行代码中,我将其转换为modelsBuilder生成的一个SponsorPage,这样我就可以直接访问这些属性,而不是teamPlayerPage.赞助商.ValuesomePropValue.Thank you@PaulAdam,我会看一看。请容忍我,因为这个概念对我来说是相当新的。将反馈我的解决方案,如果我得到了它,该方法甚至不需要是泛型的。只需将其声明为private SponsorListItem GetProfileSponsorISponsor profilePage。实际上,您也可以首先传入赞助商对象。谢谢这两个,我将研究您的建议。我可能遇到的一个问题是im使用Umbraco和models builder,这些类型是从构建和自动生成的文档类型中定义的。我的大部分逻辑都在控制器中,与生成的models@Paul,所有自动生成的类都标记为分部,可以在单独的分部文件中进行扩展,ModelsBuilder自动生成的文件和自定义文件必须具有相同的命名空间。使用这种方法,你可以很容易地实现上面提出的逻辑。这种方法甚至不需要是泛型的。只需将其声明为private SponsorListItem GetProfileSponsorISponsor profilePage。实际上,您也可以首先传入赞助商对象。谢谢这两个,我将研究您的建议。我可能遇到的一个问题是im使用Umbraco和models builder,这些类型是从构建和自动生成的文档类型中定义的。我的大部分逻辑都在控制器中,与生成的models@Paul,所有自动生成的类都标记为分部,可以在单独的分部文件中进行扩展,ModelsBuilder自动生成的文件和自定义文件必须具有相同的命名空间。使用这种方法,您可以轻松地实现上面提出的逻辑。