我有這樣的代碼:實現擴展方法
namespace Zinc.Web.Extensions.DataModel.Training
{
public static class TrainingModuleProgressStateDataModelExtentions
{
public static string GetHintText(String aString)
{
//must still make up astring here
return aString;
}
}
}
namespace Zinc.DataModels.Training
{
public class TrainingModuleProgressStateDataModel
{
public IEnumerable<UserTrainingPointsDataModel> UserTrainingPoints { get; set; }
}
}
public class UserTrainingPointsDataModel
{
public virtual int InteractionType { get; set; }
public virtual int Points { get; set; }
public virtual string Name { get; set; }
public virtual string IncentiveTrainingModuleOptionName { get; set; }
}
在我的倉庫
我添加到UserTrainingPoints:
string RawPoints = row["RawPoints"].ToString();
string[] rawPoints = RawPoints.Split(new char[] { '|' });
List<UserTrainingPointsDataModel> points = new List<UserTrainingPointsDataModel>();
foreach (var RawPoint in rawPoints)
{
string[] entry = RawPoint.Split(new char[] { ',' });
var point = new UserTrainingPointsDataModel();
point.Name = entry[0];
point.Points = Convert.ToInt32(entry[1]);
point.InteractionType = Convert.ToInt32(entry[2]);
point.IncentiveTrainingModuleOptionName = entry[3];
points.Add(point);
}
trainingModuleProgressState.UserTrainingPoints = points;
data.Add(trainingModuleProgressState);
在我看來,我需要調用其將在UserTrainingPoints值的擴展方法組成一個字符串,然後我將在工具提示中顯示。
我的問題是我如何實現擴展方法,所以我可以在我的視圖中調用它?
我的視圖代碼:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Zinc.Models.Training.TrainingModuleProgressStateDataModelResults>" %>
<%@ Import namespace="Zinc.Web.Extensions.DataModel.Training" %> //added this
<% if (module.HasAssessment)
{ %>
<div class="<%: moduleStateClass %>"> </div>
<div class="<%: moduleScoreClass %>"><%: module.ModuleScore %>%</div>
<% Zinc.Web.Extensions.DataModel.Training.TrainingModuleProgressStateDataModelExtentions.GetHintText(module.UserTrainingPoints); %> //still not correct here
你向我們展示了不包含任何擴展方法的代碼。你想成爲一種擴展方法的方法是什麼?哪種類型? –
類名建議'TrainingModuleProgressStateDataModelExtentions.GetHintText'應該是一個擴展方法,但它只接受一個字符串作爲其唯一參數。這看起來不正確。嘗試讓它不首先使用擴展方法,然後將其轉換爲擴展方法。 – hvd