Click here to Skip to main content
15,915,093 members
Home / Discussions / C#
   

C#

 
GeneralRe: Best way to store methods in a database? Pin
kalberts10-Jun-20 10:58
kalberts10-Jun-20 10:58 
QuestionMulti programming language Pin
Member 148535394-Jun-20 6:13
Member 148535394-Jun-20 6:13 
AnswerRe: Multi programming language Pin
Richard MacCutchan4-Jun-20 6:15
mveRichard MacCutchan4-Jun-20 6:15 
AnswerRe: Multi programming language Pin
OriginalGriff4-Jun-20 6:35
mveOriginalGriff4-Jun-20 6:35 
AnswerRe: Multi programming language Pin
Pete O'Hanlon4-Jun-20 21:43
mvePete O'Hanlon4-Jun-20 21:43 
GeneralRe: Multi programming language Pin
OriginalGriff4-Jun-20 21:50
mveOriginalGriff4-Jun-20 21:50 
GeneralRe: Multi programming language Pin
Pete O'Hanlon4-Jun-20 22:02
mvePete O'Hanlon4-Jun-20 22:02 
AnswerRe: Multi programming language Pin
Patrice T5-Jun-20 13:33
mvePatrice T5-Jun-20 13:33 
QuestionMulti programming language Pin
Member 148535394-Jun-20 5:38
Member 148535394-Jun-20 5:38 
AnswerRe: Multi programming language Pin
Richard Deeming4-Jun-20 5:45
mveRichard Deeming4-Jun-20 5:45 
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 
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 
I also tried the following, which is based on .net assembly - C# CompilerResults GenerateInMemory? - Stack Overflow[^], but it appears to compile even slower (I measured 208 ms):
C#
public static class MyClass
{
    public static void Example()
    {
        Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
        dynamic instance = CompileSource(
            "namespace Test" +
            "{" +
            "    public class DynamicCompile" +
            "    { " +
            "       public void logHelloThere()" +
            "       {" +
            "           System.Diagnostics.Debug.WriteLine(\"Hello there\");" +
            "       }" +
            "    }" +
            "}").TryLoadCompiledType("Test.DynamicCompile");
        instance.logHelloThere();
        long elapsedMs = watch.ElapsedMilliseconds;
        Debug.WriteLine("Elapsed: " + elapsedMs);
    }


    public static CompilerResults CompileSource(string sourceCode)
    {
        var csc = new CSharpCodeProvider(
            new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } });

        var referencedAssemblies =
                AppDomain.CurrentDomain.GetAssemblies()
                .Where(a => !a.FullName.StartsWith("mscorlib", StringComparison.InvariantCultureIgnoreCase))
                .Where(a => !a.IsDynamic) //necessary because a dynamic assembly will throw and exception when calling a.Location
                .Select(a => a.Location)
                .ToArray();

        var parameters = new CompilerParameters(referencedAssemblies);

        return csc.CompileAssemblyFromSource(parameters, sourceCode);
    }


    public static object TryLoadCompiledType(this CompilerResults compilerResults, string typeName, params object[] constructorArgs)
    {
        if (compilerResults.Errors.HasErrors)
        {
            Debug.WriteLine("Can not TryLoadCompiledType because CompilerResults.HasErrors");
            return null;
        }

        var type = compilerResults.CompiledAssembly.GetType(typeName);

        if (null == type)
        {
            Debug.WriteLine("Compiled Assembly does not contain a type [" + typeName + "]");
            return null;
        }
        return Activator.CreateInstance(type, constructorArgs);
    }
}

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 

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.