这是另一篇文章的延续。我试图创建一个界面,让我遍历一组对象,并访问对象属性的名称。
Report
对象将有ReportSections
。 ReportSection
将有一个ICollection项目,这些项目将根据使用情况而变化。
以下是我现在尝试定义的方式。
public interface IReport
{
string ReportName { get; set; }
ICollection<IReportSection> ReportSections { get; }
}
public interface IReportSection
{
string ReportSectionName { get; set; }
ICollection ReportItems { get; }
}
public abstract class ReportBase : IReport
{
virtual public string ReportType { get; set; }
virtual public string ReportName { get; set; }
virtual public ICollection<IReportSection> ReportSections { get; set; }
}
public abstract class ReportSectionBase : IReportSection
{
public string ReportSectionName { get; set; }
public ICollection ReportItems { get; set; }
}
在我的代码中,我会这样做:
public class BookAffiliates : ReportSectionBase
{
public override string ReportSectionName { get { return "Book Affiliates"; } }
public override ICollection ReportItems { get; set; }
}
public class SomeClass
{
public ICollection<AuthorsViewModel> Authors { get; set; }
public ICollection<ProjectSubmissionViewModel> Submissions { get; set; }
public string ProcessAuthorsReport()
{
var report = new ContribAuthorsReport{ ReportType = "CSV" };
var authorAffil = new BookAffiliates {ReportItems = Authors };
report.ReportSections.Add(chapAffil);
var submissionAffil = new BookAffiliates {ReportItems = Submissions};
report.ReportSections.Add(submissionAffil );
return RenderReport(report)
}
}
在RenderReport
中,我想浏览这些集合并使用PropertyNames:
private string RenderReport(ReportBase currentReport)
{
var reportBody = new StringBuilder();
reportBody.Append(currentReport.ReportName + Environment.NewLine + Environment.NewLine);
foreach (var thisSection in currentReport.ReportSections)
{
reportBody.Append(thisSection.ReportSectionName + Environment.NewLine);
/// ---- Here! Here! I don't know what type, I want the
/// code to get the type based on ReportSectionBase<T>
var firstItem = thisSection.ReportItems.OfType<???Type???>().FirstOrDefault();
// I would actually like to go through each property of
// the ReportItem type and list it here.
foreach(var prop in firstItem.GetType().GetProperties())
{
reportBody.AppendLine(string.Format("{0}:{1}" prop.Name, prop.Value));
}
}
return reportBody.ToString();
}
我不确定如何最好地定义它。我很确定我之前已经做过,但它没有找到我。
答案 0 :(得分:0)
你会使用Reflection来做到这一点。
int BBGameCenter::GameCenterState(){
return _state;
}
void BBGameCenter::ShowLeaderboard( String leaderboard_ID ){
if( _state!=2 ) return;
GKLeaderboardViewController *vc=[[GKLeaderboardViewController alloc] init];
if( !vc ) return;
vc.leaderboardDelegate=(id)_delegate;
vc.timeScope=GKLeaderboardTimeScopeToday;
vc.category=leaderboard_ID.ToNSString();
_state=3;
/
/
[BBIosGame::IosGame()->GetUIAppDelegate()->viewController presentModalViewController:vc animated:YES];
}
void BBGameCenter::ReportScore( int value,String leaderboard_ID ){
if( _state!=2 ) return;
GKScore *score=[[GKScore alloc] initWithCategory:leaderboard_ID.ToNSString()];
score.value=value;
score.context=0;
[score reportScoreWithCompletionHandler:^(NSError *error){} ];
}
void BBGameCenter::ShowAchievements(){
if( _state!=2 ) return;
GKAchievementViewController *vc=[[GKAchievementViewController alloc] init];
if( !vc ) return;
vc.achievementDelegate=(id)_delegate;
_state=4;
/
/
[BBIosGame::IosGame()->GetUIAppDelegate()->viewController presentModalViewController:vc animated:YES];
}
GKAchievement *BBGameCenter::FindAchievement( String id ){
if( !_achievements ) return 0;
NSString *str=id.ToNSString();
int n=[_achievements count];
for( int i=0;i<n;++i ){
GKAchievement *achievement=[_achievements objectAtIndex:i];
if( [achievement.identifier isEqualToString:str] ) return achievement;
}
return 0;
}
void BBGameCenter::ReportAchievement( float percent,String achievement_ID ){
if( _state!=2 ) return;
GKAchievement *achievement=FindAchievement( achievement_ID );
if( !achievement ){
achievement=[[GKAchievement alloc] initWithIdentifier:achievement_ID.ToNSString()];
[_achievements addObject:achievement];
}
achievement.percentComplete=percent;
achievement.showsCompletionBanner=true;
[achievement reportAchievementWithCompletionHandler:^(NSError *error){} ];
}
float BBGameCenter::GetAchievementPercent( String achievement_ID ){
GKAchievement *achievement=FindAchievement( achievement_ID );
if( !achievement ) return 0;
return achievement.percentComplete;
}
void BBGameCenter::GameCenterViewControllerDidFinish( UIViewController *vc ){
_state=2;
/
/
[BBIosGame::IosGame()->GetUIAppDelegate()->viewController dismissModalViewControllerAnimated:YES];
}
答案 1 :(得分:0)
花了一段时间,很多问题,并弄清楚我真正想问的问题。我想出了这个。
以下是我的接口和基类:
DataContext
我创建这样的对象:
public class ReportBase
{
public ReportBase()
{
ReportSections = new List<IReportSection>();
}
public string ReportType { get; set; }
public string ReportName { get; set; }
public ICollection<IReportSection> ReportSections { get; set; }
}
public interface IReportSection
{
string ReportSectionName { get; }
ICollection ReportItems { get; set; }
}
public class ReportSection<T> : IReportSection
{
public string ReportSectionName { get; set; }
public ICollection<T> ReportItems { get; set; }
ICollection IReportSection.ReportItems
{
get { return ReportItems as ICollection; }
set { ReportItems = value as ICollection<T>; }
}
}
然后我遍历这样的对象:
public ReportBase GetContribAuthorsReport
(
ICollection<ProjectAffiliateViewModel> projectAffiliates,
ICollection<SubmissionAffiliateViewModel> submissionAffiliates
)
{
//var caReport = new ContributingAffiliates("CSV", projectAffiliates, submissionAffiliates);
var caReport = new ReportBase { ReportType = "CSV", ReportName = "Reviewers' Contact Information" };
caReport.ReportSections.Add(new ReportSection<ProjectAffiliateViewModel> { ReportItems = projectAffiliates });
caReport.ReportSections.Add(new ReportSection<SubmissionAffiliateViewModel> { ReportItems = submissionAffiliates });
return caReport;//.Report;
}
这给了我现在所需要的东西。对不起,我一开始并不是那么清楚,谢谢你的帮助。