简体   繁体   English

具有可选通用参数的通用方法

[英]Generic Method with optional generic parameter

How do I create a generic method with an optional generic type? 如何创建具有可选泛型类型的泛型方法? This is what I have right now, which works 这就是我现在所拥有的,这是有效的

 public GridViewColumn<T> Column<D>(String HeaderText, D decorator) where D: IColumnDecorator, new()
        {
            GridViewColumn<T> column = new GridViewColumn<T>();
            column.HeaderText = HeaderText;
            column.Decorator = new D();
            return column;
        }

As you can see, I need to instantiate the type D (implements IColumnDecorator) inside the Column() method. 如您所见,我需要实例化Column()方法中的类型D(实现IColumnDecorator)。

The issue is, the type "D" is optional. 问题是类型“ D”是可选的。 If Null, I want to explicitly use a default ColumnDecorator that I have. 如果是Null,我想明确使用我拥有的默认ColumnDecorator。 something like 就像是

 public GridViewColumn<T> Column<D>(String HeaderText, D decorator) where D: IColumnDecorator, new()
        {
            GridViewColumn<T> column = new GridViewColumn<T>();
            column.HeaderText = HeaderText;
            if(decorator ==null)
            {
               column.Decorator = new DefaultColumnDecorator();
            }
            else{
               column.Decorator = new D();
            }
            return column;
        }

Please help. 请帮忙。 Thanks! 谢谢!

[Edit]. [编辑]。

Here is how I want to use it in razor MVC if I have a custom IColumnDecorator implementation 如果我有一个自定义IColumnDecorator实现,这就是我想在razor MVC中使用它的方式

@Model.[IEnumerable].Grid(grid=>{
      ..
      ...
      grid.columns(
         grid.Column<MyOwnColumnDecorator>("FirstColumn")
      )
});

If I don't have any and want to use default, then I want to be able to do something like 如果我没有任何内容并想要使用默认值,那么我希望能够执行类似的操作

@Model.[IEnumerable].Grid(grid=>{
          ..
          ...
          grid.columns(
             grid.Column("FirstColumn",null) or simply grid.Column("FirstColumn"); 
          )
    });

In your current code, you don't need the decorator parameter since you create a new instance of D and use that instead. 在当前代码中,由于创建了D的新实例并使用了它,因此不需要decorator参数。

public GridViewColumn<T> Column<D>(String HeaderText) where D: IColumnDecorator, new()
{
    GridViewColumn<T> column = new GridViewColumn<T>();
    column.HeaderText = HeaderText;
    column.Decorator = new D();
    return column;
}

If you have a default parameter type to use, you don't need to use generics: 如果要使用默认参数类型,则无需使用泛型:

public GridViewColumn<DefaultColumnDecorator> Column(String headerText)
{
    return Column<DefaultColumnDecorator>(headerText);
}

Alternatively you could keep the parameter and remove the new() constraint: 或者,您可以保留参数并删除new()约束:

public GridViewColumn<T> Column<D>(String HeaderText, D decorator) where D : IColumnDecorator
{
    GridViewColumn<T> column = new GridViewColumn<T>();
    column.HeaderText = HeaderText;
    column.Decorator = decorator;
    return column;
}

public GridViewColumn<DefaultColumnDecorator> Column(String headerText)
{
    return Column(headerText, new DefaultColumnDecorator());
}

Use a default parameter: 使用默认参数:

public GridViewColumn<T> Column<D,T>(string HeaderText, D decorator = null)
    where D : IColumnDecorator, class, new()

You shouldn't be instantiating type D from the Column method. 您不应该从Column方法实例化类型D Instead, you should let the caller pass it in. 相反,你应该让调用者传入它。

 public GridViewColumn<T> Column(String HeaderText, IColumnDecorator decorator) 
        {
            GridViewColumn<T> column = new GridViewColumn<T>();
            column.HeaderText = HeaderText;
            if(decorator ==null)
            {
               column.Decorator = new DefaultColumnDecorator();
            }
            else{
               column.Decorator = decorator;
            }
            return column;
        }

The rationale is here: http://en.wikipedia.org/wiki/Dependency_injection 理由如下: http//en.wikipedia.org/wiki/Dependency_injection

After playing with some options I made a sample that works with the following code: 在玩了一些选项后,我制作了一个使用以下代码的示例:

public GridViewColumn<TResult> Column<TColumn>(string HeaderText, TColumn decorator = null) where TColumn : class, IColumnDecorator
{
    GridViewColumn<TResult> column = new GridViewColumn<TResult>();
    column.HeaderText = HeaderText;

     if(decorator == null)
     {
        column.Decorator = new DefaultColumnDecorator();
     }
     else
     {
        column.Decorator = decorator;  
     }     

    return column;
}

In order to make this work you need to consider the following: 为了使这项工作,您需要考虑以下几点:

  • You need to add the class restriction in order to be able to define your parameter as null. 您需要添加类限制,以便能够将参数定义为null。
  • You should not use default(TColumn) since it will return null because of the class restriction. 您不应该使用default(TColumn),因为由于类限制,它将返回null。
  • For some reason if you use the "? :" syntax the compiler does not accept that you instantiate the DefaultColumnDecorator type but if you use a regular if statement it works. 出于某种原因,如果使用“?:”语法,编译器不接受您实例化DefaultColumnDecorator类型,但如果使用常规if语句则它可以工作。
  • It seems the new() restriction is not necessary for this to work. 似乎新的()限制不是必须的。

In my sample I was able to call this method in the following two ways with the same result: 在我的示例中,我能够通过以下两种方式调用此方法,结果相同:

@Model.[IEnumerable].Grid(grid=>{
      ..
      ...
      grid.columns(
         grid.Column<MyOwnColumnDecorator>("FirstColumn",null);
      )
});

@Model.[IEnumerable].Grid(grid=>{
      ..
      ...
      grid.columns(
         grid.Column<MyOwnColumnDecorator>("FirstColumn");
      )
});

Good luck! 祝好运!

There's a lot of ways to do what you're trying to do, how about asking the caller to pass a way to make a decorator? 有很多方法可以做你正在尝试做的事情,如何要求调用者通过一种方式来制作装饰器?

public GridViewColumn<T> Column(string HeaderText, Func<IColumnDecorator> decoratorGenerator)
{
  GridViewColumn<T> column = new GridViewColumn<T>();
  column.HeaderText = HeaderText;
  column.Decorator = decoratorGenerator != null ? decoratorGenerator()
    : new DefaultColumnDecorator() ;
  return column;
}

I see two things you are missing. 我看到你遗失的两件事。 One is a default parameter setting decorator = null and the other is the use of default(T) . 一个是默认参数设置decorator = null ,另一个是default(T) I have rewrote yours as follows but I can't test it obviously - should be close though. 我把你的重写如下,但我显然无法测试它 - 应该尽可能接近。

public GridViewColumn<TResult> Column<TColumn>(string HeaderText, TColumn decorator = null) where TColumn : IColumnDecorator, new()
{
    GridViewColumn<TResult> column = new GridViewColumn<TResult>();
    column.HeaderText = HeaderText;
    column.Decorator = decorator == null ? new DefaultColumnDecorator() : default(TColumn);

    return column;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM