使用布尔条件。 [英] Working with boolean conditions.

查看:147
本文介绍了使用布尔条件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



是否有任何方法可以将字符串转换为布尔值。请看下面(像这样)。

Hi,
Is there is any way for converting a statement in string to Boolean value. Please see below (Like this).

string CndtnStmnt = "(Textbox1.text == \"god\" )";
if (Convert.ToBoolean(CndtnStmnt))
{
    //Do Something
}



先谢谢。


Thanks in Advance.

推荐答案

你好Manu Prasad,



我准备了一个方法,可以将表达式语句作为参数执行,并给出布尔结果你在期待。



您需要下面列出的命名空间: -

Hi Manu Prasad,

I have prepared one method which can take the expression statement to execute as parameter and give the boolean result as you are expecting.

You need the namespaces as listed below :-
using System;
using System.Text;
using System.CodeDom;
using Microsoft.CSharp;
using System.Reflection;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using System.Collections.Generic;





我希望以下方法对您专门为您的帖子创建有用。





I hope the below method will be helpful for you which created exclusively for your post.

public bool TestDymanicCode(string strCodeSnippetToExecute)
        {
            bool bResult = false;
            string strSourceTemplate =
            @"using System; 
                      using System.Windows.Forms; 

                      namespace TestNamespace { 
                         public static class TestClass { 
                           public static bool Execute() {
                             @Placeholder
                           }
                         }
                       }";
            string strReplacedCode = strSourceTemplate.Replace("@Placeholder", strCodeSnippetToExecute);
            CodeSnippetCompileUnit objCodeSnippetCompileUnit = new CodeSnippetCompileUnit(strReplacedCode);

            using (CSharpCodeProvider provider =
                new CSharpCodeProvider(new Dictionary<string,> { { "CompilerVersion", "v3.5" } }))
            {
                CompilerParameters objCompilerParameters = new CompilerParameters();
                objCompilerParameters.ReferencedAssemblies.Add("System.dll");
                objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
                objCompilerParameters.GenerateExecutable = false;
                objCompilerParameters.GenerateInMemory = true;
                objCompilerParameters.IncludeDebugInformation = false;

                CompilerResults objCompilerResults = provider.CompileAssemblyFromDom(objCompilerParameters, objCodeSnippetCompileUnit);

                if (!objCompilerResults.Errors.HasErrors)
                {
                    Type type = objCompilerResults.CompiledAssembly.GetType("TestNamespace.TestClass");
                    MethodInfo method = type.GetMethod("Execute");

                    /* YOU CAN GET YOUR BOOLEAN RESULT HERE. */
                    object val = method.Invoke(null, new object[] { });
                    bResult = Convert.ToBoolean(val);
                }
                else
                {
                    StringBuilder objSB = new StringBuilder();
                    foreach (CompilerError objCompilerError in objCompilerResults.Errors)
                        objSB.AppendFormat("Error found in line {0}:\n\n{1}", objCompilerError.Line, objCompilerError.ErrorText);
                    MessageBox.Show(objSB.ToString(), "Compiler Error");
                }
            }
            return bResult;
        }





上面的方法可以像这样调用: -



The upper method can be called like this :-

string strReturnCodeSnippet = "return (\"" + txtTestDynamic.Text + "\" == \"Test\");";
            MessageBox.Show(TestDymanicCode(strReturnCodeSnippet).ToString());





希望你会对此有所帮助。



Hope you will be helpful by this.


如果你想尝试在VB.NET中创建通用的'Eval命令的等效命令,那么你需要做很多工作。使用CodeDom,构建表达式树等是很棘手的。



CodeProject成员Jing Lu,ReoGrid的作者,已经发布了一个非常强大的C#脚本语言,你可以使用它:[ ^ ]。



在这个Google搜索页面上,您可以找到几个CodeProject文章的链接,这些文章显示了C#中string => execute-code的各种技术,包括在C#中使用VB.NET的Eval的想法:[ ^ ]。
If you want to try and create the equivalent of the general-purpose 'Eval command in VB.NET, you are taking on a lot of work. Using CodeDom, building Expression Trees, etc. is tricky.

CodeProject member Jing Lu, author of ReoGrid, has published a very robust scripting language for C# that you could use: [^].

On this Google search page you will find links to several CodeProject articles showing various techniques for string=>execute-code in C# including the idea of using VB.NET's Eval in C#: [^].


不,或者至少不容易。

因为你展示的代码引用了表单本身的控件和属性,你需要编译你想要评估的行并执行它直接作为你的代码的一部分。



这是可能的 - 非常非常讨厌,但可能 - 但可能更便宜复杂的方式来做你想要实现的事情而不是像这样的事情。

如果我是你,我会寻找它!
No, or at least not easily.
Because the code you show references controls and properties on the form itself, you would need to compile the line you are trying to evaluate and execute it directly as part of your code.

This is possible - very, very nasty, but possible - but there is probably a much less expensive and complex way to do what you are trying to achieve than something like this.
I'd look for that instead if I was you!


这篇关于使用布尔条件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆