C#-有关[Flags]枚举的帮助&;扩展方法
我有以下枚举:C#-有关[Flags]枚举的帮助&;扩展方法,c#,enums,extension-methods,flags,enum-flags,C#,Enums,Extension Methods,Flags,Enum Flags,我有以下枚举: [Flags] public enum PostAssociations { None = 0x0, User = 0x1, Comments = 0x2, CommentsUser = 0x3 } 首先,我不确定这些标志是否正确 我这样做是为了能够流畅地为实体框架定义“Include”(因为EF Include方法接受一个字符串,我不想将其公开给UI) 所以我需要它,这样我的服务层就可以接受一个后关联,在我的服务层中,我使用扩展方法将其转换为字
[Flags]
public enum PostAssociations
{
None = 0x0,
User = 0x1,
Comments = 0x2,
CommentsUser = 0x3
}
首先,我不确定这些标志是否正确
我这样做是为了能够流畅地为实体框架定义“Include”(因为EF Include方法接受一个字符串,我不想将其公开给UI)
所以我需要它,这样我的服务层就可以接受一个后关联,在我的服务层中,我使用扩展方法将其转换为字符串[]。(然后,我的回购协议将其拆分,以便执行.Include)
我对Flags Enum做的不多,因此我为我的无知道歉。:)
这是我想要的“真值表”(枚举值,转换字符串[])
不能有无注释的注释
所以我需要三件事的帮助:
谢谢大家。如果您创建了带有标志的枚举:
[Flags]
public enum PostAssociations
{
None = 0x0,
User = 0x1,
Comments = 0x2,
CommentsUser = User|Comments,
}
这会更有意义。在您当前的代码中,我不理解您试图实现的目标
否则,我认为您根本不需要基于标志的枚举…如果您创建了带有标志的枚举:
[Flags]
public enum PostAssociations
{
None = 0x0,
User = 0x1,
Comments = 0x2,
CommentsUser = User|Comments,
}
这会更有意义。在您当前的代码中,我不理解您试图实现的目标
否则,我认为您根本不需要基于标志的枚举…我的坏蛋,我对标志枚举的经验不足导致了一个令人困惑的问题 我的方案对标志枚举无效 相反,我选择使用PostAssociations[] 我的服务层:
public Post FindSingle(int postId, PostAssociations[] postAssociations)
{
return repo.Find(postAssocations.ToEfInclude()).WithId(postId);
}
扩展方法:
public static string[] ToEfInclude(this PostAssociations[] postAssocations)
{
if (postAssocations == null) return null;
List<string> includeAssociations = new List<string>();
if (postAssocations.Contains(PostAssociations.User))
includeAssociations.Add("User");
if (postAssocations.Contains(PostAssociations.Comments))
{
if (postAssocations.Contains(PostAssociations.CommentsUser))
{
includeAssociations.Add("Comments.User");
}
includeAssociations.Add("Comments");
}
return includeAssociations.ToArray();
}
我的坏蛋,我对标志枚举的缺乏经验导致了一个令人困惑的问题 我的方案对标志枚举无效 相反,我选择使用PostAssociations[] 我的服务层:
public Post FindSingle(int postId, PostAssociations[] postAssociations)
{
return repo.Find(postAssocations.ToEfInclude()).WithId(postId);
}
扩展方法:
public static string[] ToEfInclude(this PostAssociations[] postAssocations)
{
if (postAssocations == null) return null;
List<string> includeAssociations = new List<string>();
if (postAssocations.Contains(PostAssociations.User))
includeAssociations.Add("User");
if (postAssocations.Contains(PostAssociations.Comments))
{
if (postAssocations.Contains(PostAssociations.CommentsUser))
{
includeAssociations.Add("Comments.User");
}
includeAssociations.Add("Comments");
}
return includeAssociations.ToArray();
}
首先,最重要的是,
0x3
绝对不是正确的值,除非你的意思是CommentsUser
代表User
和Comments
之间的联合,我怀疑你实际上不想要。你是什么意思,你不能让CommentsUser没有注释?@Kirk Woll-不,这不是我想要的。正如我所说的,我没有使用enum标志的经验-我不知道这些标志使用什么值。在这个阶段,除非有人能给出答案,否则我将使用一系列的后关联(与旗帜相反)。@John Gardner-正确。除非还提供了“Comments”(因为.User是.Comments的子集),否则无法提供“Comments.User”。首先,0x3
绝对不是正确的值,除非你的意思是让CommentsUser
代表User
和Comments
之间的联合,我怀疑你实际上并不想要。你是什么意思,你不能让CommentsUser没有注释?@Kirk Woll-不,这不是我想要的。正如我所说的,我没有使用enum标志的经验-我不知道这些标志使用什么值。在这个阶段,除非有人能给出答案,否则我将使用一系列的后关联(与旗帜相反)。@John Gardner-正确。除非还提供了“Comments”(因为.User是.Comments的子集),否则无法提供“Comments.User”。是的,我认为我不应该在这里使用标志枚举。你的建议是有道理的,但在我的场景中没有(因为我没有正确地解释)。CommentsUser是一个单独的值(不是用户+注释)。因此,我认为我不应该使用标志。我将坚持使用PostAssociations[]。您可以在任何枚举上使用位运算符,假设它们是二的幂。你不需要flags属性,除非你想让它很好地工作(还有一些其他的特性,但是我不记得了)。是的,我不认为我应该在这里使用flag枚举。你的建议是有道理的,但在我的场景中没有(因为我没有正确地解释)。CommentsUser是一个单独的值(不是用户+注释)。因此,我认为我不应该使用标志。我将坚持使用PostAssociations[]。您可以在任何枚举上使用位运算符,假设它们是二的幂。你不需要flags属性,除非你想很好地工作(还有一些其他的特性,但是我不记得了)。为了可用性,你的FindSingle()方法可以实现为FindSingle(int-id,params-PostAssociations[]args)。为了可用性,你的FindSingle()方法可以实现为FindSingle(int id,参数PostAssociations[]args)