本文介绍了在一个枚举传递方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我宣布一个枚举:

public enum SupportedPermissions
{
    basic,
    repository,
    both
}

我也有一个POCO这样的:

I also have a POCO like this:

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:

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;
}



我将如何创建一个枚举的参数,我将如何分配,像在我的伪代码 SupportedPermissions = supportedPermissions.basic 让我的文件实例都有一个SupportedPermissions设置呢?

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

推荐答案

更改的CreateFile 方法的签名,以期望 SupportedPermissions 价值而非纯枚举。

Change the signature of the CreateFile method to expect a SupportedPermissions value instead of plain 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;
}



然后当你调用你的方法,你传递 SupportedPermissions 价值,你的方法。

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

这篇关于在一个枚举传递方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 22:49