C# 传入一个枚举作为方法参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18955462/
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-10 13:40:32  来源:igfitidea点击:

Pass in an enum as a method parameter

c#enums

提问by Mike Barnes

I have declared an enum:

我已经声明了一个枚举:

public enum SupportedPermissions
{
    basic,
    repository,
    both
}

I also have a POCO like this:

我也有这样的 POCO:

public class File
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public SupportedPermissions SupportedPermissions { get; set; }      
}

Now I would like to create a method that I can use to create a new File object with:

现在我想创建一个方法,我可以用它来创建一个新的 File 对象:

public string CreateFile(string id, string name, string description, Enum supportedPermissions)
{
    file = new File
    {  
        Name = name,
        Id = id,
        Description = description,
        SupportedPermissions = supportedPermissions.basic
    };

    return file.Id;
}

How would I create the parameter for the enum and how would I assign that like in my pseudo code SupportedPermissions = supportedPermissions.basicso that my File instance has a SupportedPermissions set to it?

我将如何为枚举创建参数,以及如何在我的伪代码中分配它,SupportedPermissions = supportedPermissions.basic以便我的 File 实例设置了 SupportedPermissions ?

采纳答案by Jehof

Change the signature of the CreateFilemethod to expect a SupportedPermissionsvalue instead of plain Enum.

CreateFile方法的签名更改为SupportedPermissions期望值而不是普通的 Enum。

public string CreateFile(string id, string name, string description, SupportedPermissions supportedPermissions)
{
    file = new File
    {  
        Name = name,
        Id = id,
        Description = description,
        SupportedPermissions = supportedPermissions
    };

    return file.Id;
}

Then when you call your method you pass the SupportedPermissionsvalue to your method

然后当你调用你的方法时,你将SupportedPermissions值传递给你的方法

  var basicFile = CreateFile(myId, myName, myDescription, SupportedPermissions.basic);

回答by Daniel Hilgarth

If you want to pass in the value to use, you have to use the enum type you declared and directly use the supplied value:

如果要传入要使用的值,则必须使用您声明的枚举类型并直接使用提供的值:

public string CreateFile(string id, string name, string description,
              /* --> */  SupportedPermissions supportedPermissions)
{
    file = new File
    {  
        Name = name,
        Id = id,
        Description = description,
        SupportedPermissions = supportedPermissions // <---
    };

    return file.Id;
}

If you instead want to use a fixed value, you don't need any parameter at all. Instead, directly use the enum value. The syntax is similar to a static member of a class:

如果您想使用固定值,则根本不需要任何参数。相反,直接使用枚举值。语法类似于类的静态成员:

public string CreateFile(string id, string name, string description) // <---
{
    file = new File
    {  
        Name = name,
        Id = id,
        Description = description,
        SupportedPermissions = SupportedPermissions.basic // <---
    };

    return file.Id;
}

回答by Moo-Juice

public string CreateFile(string id, string name, string description, SupportedPermissions supportedPermissions)
{
    file = new File
    {  
       Name = name,
        Id = id,
        Description = description,
        SupportedPermissions = supportedPermissions
    };

    return file.Id;
}

回答by Ovidiu

First change the method parameter Enum supportedPermissionsto SupportedPermissions supportedPermissions.

首先将方法参数更改Enum supportedPermissionsSupportedPermissions supportedPermissions.

Then create your file like this

然后像这样创建你的文件

file = new File
{  
    Name = name,
    Id = id,
    Description = description,
    SupportedPermissions = supportedPermissions
};

And the call to your method should be

对你的方法的调用应该是

CreateFile(id, name, description, SupportedPermissions.basic);