在 C# 中只传递一个类型作为参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10955579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:55:35  来源:igfitidea点击:

Passing just a type as a parameter in C#

c#typesmethodsparameters

提问by Mark Mayo

Hypothetically it'd be handy for me to do this:

假设我这样做会很方便:

foo.GetColumnValues(dm.mainColumn, int)
foo.GetColumnValues(dm.mainColumn, string)

where the GetColumns method will call a different method inside depending on the type passed.

其中 GetColumns 方法将根据传递的类型在内部调用不同的方法。

Yes, I could do it as a boolean flag or similar, I just wondered if there was a way to perhaps pass this, and then ask:

是的,我可以把它作为一个布尔标志或类似的东西来做,我只是想知道是否有办法通过这个,然后问:

typeof(arg[1]) or similar...

typeof(arg[1]) 或类似的...

I could also override the method, use generics, etc - I know there are different ways to do this, I was just curious if this was possible.

我还可以覆盖该方法,使用泛型等 - 我知道有不同的方法可以做到这一点,我只是好奇这是否可能。

采纳答案by Reed Copsey

There are two common approaches. First, you can pass System.Type

有两种常见的方法。首先,你可以通过System.Type

object GetColumnValue(string columnName, Type type)
{
    // Here, you can check specific types, as needed:

    if (type == typeof(int)) { // ...

This would be called like: int val = (int)GetColumnValue(columnName, typeof(int));

这将被称为: int val = (int)GetColumnValue(columnName, typeof(int));

The other option would be to use generics:

另一种选择是使用泛型:

T GetColumnValue<T>(string columnName)
{
    // If you need the type, you can use typeof(T)...

This has the advantage of avoiding the boxing and providing some type safety, and would be called like: int val = GetColumnValue<int>(columnName);

这具有避免拳击并提供某种类型安全的优点,并且将被称为: int val = GetColumnValue<int>(columnName);

回答by Peter Ritchie

foo.GetColumnValues(dm.mainColumn, typeof(string))

foo.GetColumnValues(dm.mainColumn, typeof(string))

Alternatively, you could use a generic method:

或者,您可以使用通用方法:

public void GetColumnValues<T>(object mainColumn)
{
    GetColumnValues(mainColumn, typeof(T));
}

and you could then use it like:

然后你可以像这样使用它:

foo.GetColumnValues<string>(dm.mainColumn);

回答by Mark Byers

You can pass a type as an argument, but to do so you must use typeof:

您可以将类型作为参数传递,但要这样做,您必须使用typeof

foo.GetColumnValues(dm.mainColumn, typeof(int))

The method would need to accept a parameter with type Type.

该方法需要接受类型为 的参数Type



where the GetColumns method will call a different method inside depending on the type passed.

其中 GetColumns 方法将根据传递的类型在内部调用不同的方法。

If you want this behaviour then you should not pass the type as an argument but instead use a type parameter.

如果您想要这种行为,则不应将类型作为参数传递,而应使用类型参数。

foo.GetColumnValues<int>(dm.mainColumn)

回答by Kevin DiTraglia

You can do this, just wrap it in typeof()

你可以这样做,只要把它包起来 typeof()

foo.GetColumnValues(typeof(int))

public void GetColumnValues(Type type)
{
    //logic
}

回答by 500 - Internal Server Error

You can use an argument of type Type - iow, pass typeof(int). You can also use generics for a (probably more efficient) approach.

您可以使用类型类型的参数 - iow,传递 typeof(int)。您还可以将泛型用于(可能更有效)的方法。

回答by Danny Varod

foo.GetColumnValues(dm.mainColumn, typeof(int));
foo.GetColumnValues(dm.mainColumn, typeof(string));

Or using generics:

或者使用泛型:

foo.GetColumnValues<int>(dm.mainColumn);
foo.GetColumnValues<string>(dm.mainColumn);

回答by Boris Detry

Use generic types !

使用泛型!

  class DataExtraction<T>
{
    DateRangeReport dateRange;
    List<Predicate> predicates;
    List<string> cids;

    public DataExtraction( DateRangeReport dateRange,
                           List<Predicate> predicates,
                           List<string> cids)            

    {
        this.dateRange = dateRange;
        this.predicates = predicates;
        this.cids = cids;
    }
}

And call it like this :

并这样称呼它:

  DataExtraction<AdPerformanceRow> extractor = new DataExtraction<AdPerformanceRow>(dates, predicates , cids);