Click here to Skip to main content
15,884,388 members
Home / Discussions / C#
   

C#

 
AnswerRe: Multi programming language Pin
OriginalGriff4-Jun-20 6:34
mveOriginalGriff4-Jun-20 6:34 
AnswerRe: Multi programming language Pin
Pete O'Hanlon4-Jun-20 21:42
mvePete O'Hanlon4-Jun-20 21:42 
GeneralRe: Multi programming language Pin
OriginalGriff4-Jun-20 21:48
mveOriginalGriff4-Jun-20 21:48 
AnswerRe: Multi programming language Pin
F-ES Sitecore5-Jun-20 2:37
professionalF-ES Sitecore5-Jun-20 2:37 
AnswerRe: Multi programming language Pin
Patrice T5-Jun-20 13:35
mvePatrice T5-Jun-20 13:35 
QuestionAre there any good apple ipad c# compilers? Pin
Ultra9603-Jun-20 1:20
Ultra9603-Jun-20 1:20 
AnswerRe: Are there any good apple ipad c# compilers? Pin
OriginalGriff3-Jun-20 1:25
mveOriginalGriff3-Jun-20 1:25 
QuestionHow can I compile code in runtime faster than CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom? Pin
arnold_w2-Jun-20 5:31
arnold_w2-Jun-20 5:31 
I need to parse numerical expressions, e.g. (UInt32)(512 * 1024) that we can assume comes from a WinForm TextBox during runtime. I tried using the following:
C#
DataTable loDataTable = new DataTable();
DataColumn loDataColumn = new DataColumn("Eval", typeof(double), expression);
loDataTable.Columns.Add(loDataColumn);
loDataTable.Rows.Add(0);
return (UInt32)(double)(loDataTable.Rows[0]["Eval"]);
but this doesn't work because of the cast. So instead I tried the following, which is based on possible to execute code contained in a string ? - C# / C Sharp[^] :
C#
public static bool parseDifficultExpression(string difficultExpression, out UInt32 parsedValue)
{
    try
    {
        string[] usingList = new string[] { "System",
                                            "System.Text",
                                            "System.Diagnostics" };
        string methodAsString = "    returnValue = " + difficultExpression + ";";
        string notUsed = "";
        parsedValue = (UInt32)Utilities.executeMethodAsString(usingList, methodAsString, notUsed, typeof(UInt32));
        return true;
    }
    catch (Exception)
    {
        parsedValue = 0xFFFFFFFF;
        return false;
    }
}

public static object executeMethodAsString(string[] usingList, string methodAsString, object args, Type returnType)
{
    string[] importList = new string[] { "System.dll" };
    string namespaceName = "myNameSpace";
    string className = "myClass";
    string methodName = "myMethod";

    //Create method
    CodeMemberMethod pMethod = new CodeMemberMethod();
    pMethod.Name = methodName;
    pMethod.Attributes = MemberAttributes.Public;
    pMethod.Parameters.Add(new CodeParameterDeclarationExpression(args.GetType(), "args"));
    pMethod.ReturnType = new CodeTypeReference(typeof(UInt32));
    string completeMethod =
        returnType + " returnValue = new " + returnType + "();" + "\r\n" +
        "try {"                                                 + "\r\n" +
            methodAsString                                      + "\r\n" +
        "} catch {"                                             + "\r\n" +
        "    returnValue = new " + returnType + "();"           + "\r\n" +
        "}"                                                     + "\r\n" +
        "return returnValue;";
    pMethod.Statements.Add(new CodeSnippetExpression(completeMethod));

//    Here you can look at what the assembled method looks like
//    Console.WriteLine(completeMethod);

    CodeTypeDeclaration pClass = new CodeTypeDeclaration(className);
    pClass.Attributes = MemberAttributes.Public;
    pClass.Members.Add(pMethod);
    CodeNamespace pNamespace = new CodeNamespace(namespaceName);
    pNamespace.Types.Add(pClass);
    for (int i = 0; i < usingList.Length; i++)
    {
        pNamespace.Imports.Add(new CodeNamespaceImport(usingList[i]));
    }

    CodeCompileUnit pUnit = new CodeCompileUnit();
    pUnit.Namespaces.Add(pNamespace);
    CompilerParameters pParams = new CompilerParameters(importList);
    pParams.GenerateInMemory = true;
    CompilerResults pResults = (new CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom(pParams, pUnit);

    if ((pResults.Errors != null) && (0 < pResults.Errors.Count))
    {
        foreach (CompilerError pError in pResults.Errors)
        {
            Debug.WriteLine(pError.ToString());
        }
        return null;
    }
    else
    {
        object typeInstance = pResults.CompiledAssembly.CreateInstance(namespaceName + "." + className);
        MethodInfo methodInfo = typeInstance.GetType().GetMethod(methodName);
        return methodInfo.Invoke(typeInstance, new object[] { args });
    }
}
This works fine, except that the line
C#
CompilerResults pResults = (new CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom(pParams, pUnit);
is extremely slow (about 80 ms) to execute. Does anybody know a workaround to achieve what I want in fast execution time?
AnswerRe: How can I compile code in runtime faster than CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom? Pin
OriginalGriff2-Jun-20 5:50
mveOriginalGriff2-Jun-20 5:50 
GeneralRe: How can I compile code in runtime faster than CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom? Pin
arnold_w2-Jun-20 9:40
arnold_w2-Jun-20 9:40 
AnswerRe: How can I compile code in runtime faster than CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom? Pin
arnold_w2-Jun-20 22:59
arnold_w2-Jun-20 22:59 
AnswerRe: How can I compile code in runtime faster than CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom? Pin
arnold_w3-Jun-20 2:39
arnold_w3-Jun-20 2:39 
AnswerRe: How can I compile code in runtime faster than CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom? Pin
Gerry Schmitz3-Jun-20 6:40
mveGerry Schmitz3-Jun-20 6:40 
GeneralRe: How can I compile code in runtime faster than CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom? Pin
arnold_w3-Jun-20 9:30
arnold_w3-Jun-20 9:30 
GeneralRe: How can I compile code in runtime faster than CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom? Pin
kalberts3-Jun-20 10:10
kalberts3-Jun-20 10:10 
GeneralRe: How can I compile code in runtime faster than CSharpCodeProvider()).CreateCompiler().CompileAssemblyFromDom? Pin
arnold_w3-Jun-20 10:34
arnold_w3-Jun-20 10:34 
QuestionConverting InAppBilling.Plugin to Amazon Pin
Exoskeletor2-Jun-20 3:52
Exoskeletor2-Jun-20 3:52 
AnswerRe: Converting InAppBilling.Plugin to Amazon Pin
OriginalGriff2-Jun-20 4:38
mveOriginalGriff2-Jun-20 4:38 
GeneralRe: Converting InAppBilling.Plugin to Amazon Pin
Exoskeletor2-Jun-20 4:56
Exoskeletor2-Jun-20 4:56 
GeneralRe: Converting InAppBilling.Plugin to Amazon Pin
OriginalGriff2-Jun-20 5:18
mveOriginalGriff2-Jun-20 5:18 
GeneralRe: Converting InAppBilling.Plugin to Amazon Pin
Exoskeletor2-Jun-20 7:40
Exoskeletor2-Jun-20 7:40 
AnswerRe: Converting InAppBilling.Plugin to Amazon Pin
Kris Lantz2-Jun-20 9:13
professionalKris Lantz2-Jun-20 9:13 
GeneralRe: Converting InAppBilling.Plugin to Amazon Pin
Exoskeletor2-Jun-20 9:24
Exoskeletor2-Jun-20 9:24 
QuestionState machine performance woes in .NET Pin
kalberts2-Jun-20 3:04
kalberts2-Jun-20 3:04 
AnswerRe: State machine performance woes in .NET Pin
kalberts2-Jun-20 3:41
kalberts2-Jun-20 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.