简体   繁体   English

条件运算符,验证问题

[英]Conditional Operators, Validation Issue

I'm currently writing a method that will do a couple of things: 我目前正在写,会做两件事情的方法

  • Verify the Operating System Version . 验证操作系统版本
  • Verify the Operating System Platform . 验证操作系统平台
  • Verify the Account isn't null . 验证帐户不为null
  • Verify the Account is in the proper role . 验证帐户是否具有适当的角色

Now, if I implement the traditional nested if it works. 现在,如果我实现了传统嵌套,那么是否可以工作。 Absolutely zero problems- However, for the sake of what I believe to be a cleaner implementation has turned into a lovely error. 绝对为零的问题-但是,为了我所认为的更清洁的实现,它变成了一个可爱的错误。

The syntax: 语法:

bool result = false;

WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal role = new WindowsPrincipal(user);

result = ((Environment.OSVersion.Platform == PlatformID.Win32NT && 
     Environment.OSVersion .Version.Major > 6 
     && role != null && role.IsInRole(WindowsBuiltInRole.Administrator) 
     ? true : false);

But I receive the following exceptions . 但是我收到以下例外

Operator && cannot be applied to the operands of type System.PlatformID and bool . 运算符&&不能应用于System.PlatformIDbool类型的操作数。

I'm really not sure why it doesn't work, it should. 我真的不知道为什么它不起作用,应该。 Am I implementing logic incorrectly or what, I'm really at a loss. 我是错误地实现了逻辑还是什么,我真的很茫然。

This syntax does work, but when I convert it to the above Conditional it doesn't. 此语法确实有效,但是当我将其转换为上述条件时,它无效。

if(Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion
    .Version.Major > 6)
{
     WindowsIdentity user = WindowsIdentity.GetCurrent();
     WindowsPrincipal role = new WindowsPrincipal(user);

     if(role != null)
     {

          if(role.IsInRole(WindowsBuiltInRole.Administrator))
          { 
               return true;
          }
     }
     return false;
}
return false;

Update: 更新:

This is where the red squiggle appears and Visual Studio gave the above mentioned error: 这是出现红色花体的地方,Visual Studio给出了上述错误:

PlatformID.Win32NT && Environment.OSVersion.Version.Major > 6

You don't actually need to use a conditional operator—in fact … ? true : false 实际上,您实际上不需要使用条件运算符- … ? true : false … ? true : false never has any effect at all. … ? true : false根本没有任何作用。

Try rewriting your code like this: 尝试像这样重写代码:

bool result = 
    (Environment.OSVersion.Platform == PlatformID.Win32NT) && 
    (Environment.OSVersion.Version.Major > 6) &&
    (role != null) && 
    (role.IsInRole(WindowsBuiltInRole.Administrator));

Your conditional could be rewritten like this: 您的条件可以这样重写:

bool result = Environment.OSVersion.Platform == PlatformID.Win32NT &&
              Environment.OSVersion.Version.Major > 6 &&
              role.IsInRole(WindowsBuiltInRole.Administrator);

Note that you can skip the 'role' null check, since it's never null in your case. 请注意,您可以跳过'role'null检查,因为在您的情况下它永远不会为null。

EDIT 编辑

As far as your update goes, the problem is this part: 就您的更新而言,问题在于此部分:

bool result = PlatformID.Win32NT; // <-- this part can't compile, it's not a boolean

I believe what you meant to write is: 我相信您要写的是:

bool result = Environment.OSVersion.Platform == PlatformID.Win32NT; // along with your other conditions

EDIT 2 编辑2

Since you've asked why doesn't your sample work (not sure what typo you have or what exactly is going on), but this code compiles as well ( NOTE: I wouldn't write it like that , just saying): 既然您已经问过为什么您的示例不起作用(不确定您有什么错别字或到底发生了什么),但是此代码也可以编译( 注意: 我不会这样写 ,只是说):

bool result = ((Environment.OSVersion.Platform == PlatformID.Win32NT &&
                Environment.OSVersion.Version.Major > 6
                && role != null && role.IsInRole(WindowsBuiltInRole.Administrator)
                    ? true
                    : false));

Not tested , you can try 未经测试,可以尝试

   bool result = false;

    WindowsIdentity user = WindowsIdentity.GetCurrent();
    WindowsPrincipal role = new WindowsPrincipal(user);

    result = (((Environment.OSVersion.Platform == PlatformID.Win32NT) && 
         (Environment.OSVersion .Version.Major > 6) 
         && (role != null) && ((role.IsInRole(WindowsBuiltInRole.Administrator) 
         )? true : false))));

    return result;

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

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