C#中参数的通配符

时间:2018-12-07 04:44:34

标签: c# polymorphism wildcard viewmodel

对于要用于生成局部视图的某些视图模型,我至少具有四个不同的类。事实是,每个局部视图都以完全相同的方式生成一个下拉列表。

public class LocalityGeneratorForm
{
    [Key]
    public int id { get; set; }
    [Display(Name = "Description of the field")]
    public Locality[] locality { get; set; }
    public virtual ICollection<Locality> Localities { get; }
}

public class GoalGeneratorForm
{
    [Key]
    public int id { get; set; }
    [Display(Name = "A different description of the field")]
    public Goal[] goal { get; set; }
    public virtual ICollection<Goal> Goals { get; }
}

public class ProcessGeneratorForm
{
    [Key]
    public int id { get; set; }
    [Display(Name = "A third description of the field")]
    public Process[] process { get; set; }
    public virtual ICollection<Process> Processes { get; }
}

public class FieldGeneratorForm
{
    [Key]
    public int id { get; set; }
    [Display(Name = "The other new description of the field")]
    public Field[] field { get; set; }
    public virtual ICollection<Field> Fields { get;  }
}

好吧,所以所有这些ViewModel确实非常相似,只是它们的ICollection类型不同。现在,要生成HTML字符串,我将使用一个带有两个参数的函数,一个是用作模板的部分视图的名称,另一个是模型。

所以我希望能够创建一个方法,该方法愿意接受以上或将来的任何类的实例,也许更多。

private string ConvertViewToString( string viewName, <GenericClass> model ) 
{
    //-- Do stuff..
    return "My HTML";
}

第一个问题是,是否可能,如果可以,如何声明泛型参数?谢谢!

2 个答案:

答案 0 :(得分:1)

如果要使用泛型方法,只需执行以下操作:

private string ConvertViewToString<T>(string viewName, T model) {
    //-- Do stuff..
    return "My HTML";
}

// How to call this method?
LocalityGeneratorForm localGenForm = new LocalityGeneratorForm(); /* Simple instancing */

// in this case, T will automatically LocalityGeneratorForm.
ConvertViewToString(some_string, localGenForm);

// or you can explicit define what T type is
ConvertViewToString<LocalityGeneratorForm>(some_string, localGenForm);

,并且如果您想限制类型参数T的类型,则可以使用where

如果您真的想限制ConvertViewToString方法将采用SomethingTopModelClass并且仅是派生类型,则可以这样使用:

private string ConvertViewToString<T>(string viewName, T model) where T: SomethingTopModelClass {
    //-- Do stuff..
    return "My HTML";
}

我的解释可能很难理解(英语错误:S),因此,建议您阅读以下文档。

C# Generics (MSDN)

C# Generics - Where (MSDN)

答案 1 :(得分:0)

我认为您需要的不是通用方法/类。实现此目的的最佳方法是修改ConvertViewToString以接受接口而不是特定的实现。将接口注入方法将在将来具有更大的灵活性。

public interface IHtmlElement {
    string ToHtmlString();
}

public class LocalityGeneratorForm : IHtmlElement
{
    [Key]
    public int id { get; set; }
    [Display(Name = "Description of the field")]
    public Locality[] locality { get; set; }
    public virtual ICollection<Locality> Localities { get; }
    public string ToHtmlString()
    {
        // convert Locality to html string representation
        return "<p>HTML element string for Locality</p>"
    }
}

public class GoalGeneratorForm : IHtmlElement
{
    [Key]
    public int id { get; set; }
    [Display(Name = "A different description of the field")]
    public Goal[] goal { get; set; }
    public virtual ICollection<Goal> Goals { get; }
    public string ToHtmlString()
    {
        // convert Goal to html string representation
        return "<p>HTML element string for Goal</p>"
    }
}

下一步,您可以通过以下方式进行ConverViewToString实现:

private string ConvertViewToString( string viewName, IHtmlElement element ) 
{
    //-- Do stuff..
    return element.ToHtmlString();
}

这是将所有内容组合在一起的方法:

var locality = new LocalityGeneratorForm();
var goal = new GoalGeneratorForm();

var htmlLocality = ConvertViewToString("some name", locality);
var htmlGoal = ConvertViewToString("some name", goal);

希望我的回答可以帮助您使实施更加灵活。