Execute code from string in net core runtime? - c#

For example we have such a class:
namespace ConsoleApp1
{
public class Program
{
public int x = 0;
public int y = 1;
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Program program = new Program();
program.RunScript(Console.ReadLine());
}
public void RunScript(string script)
{
//....
}
public void CallMethod1()
{
}
public void CallMethod2()
{
}
}
}
And in the console I want to enter an expression for execution, in the language C#:
"if(x > y){CallMethod1();}else{CallMethod2();}"
how can this expression be executed? I've seen examples with Roslyn https://learn.microsoft.com/en-us/archive/msdn-magazine/2017/may/net-core-cross-platform-code-generation-with-roslyn-and-net-core
but they call static functions or functions from the new namespace, but I need to call a function that is already in the current namespace

Related

Why use static methods in program.cs C#? [duplicate]

This question already has answers here:
When to use static methods
(24 answers)
Closed 2 years ago.
I always knew that I have to use static methods but I wonder why?
As you can see below I have to make "MigrateDatabase" Static
using System;
namespace OdeToFood
{
public class Program
{
public static void Main(string[] args)
{
MigrateDatabase();
}
private static void MigrateDatabase()
{
//.....
}
}
}
Let's just be clear, the only reason MigrateDatabase has to be static in this case is because you're calling it from a static method (Main). If instead MigrateDatabase was an instance method on a class, you could instantiate that class and call it
using System;
namespace OdeToFood
{
public class Program
{
public static void Main(string[] args)
{
var migration = new Migration();
migration.MigrateDatabase();
}
}
public class Migration
{
private void MigrateDatabase()
{
//.....
}
}
}
You could also put it as a instance method on Program if you're instantiating an instance of that class
using System;
namespace OdeToFood
{
public class Program
{
public static void Main(string[] args)
{
var program = new Program();
program.MigrateDatabase();
}
private void MigrateDatabase()
{
//.....
}
}
}

C# novice question about modifying outside variables inside a method

In the examples below I want to know a good way to make the bottom example function like the top example. I know that scope is the reason the bottom example does not work.
I am interested in doing this so I can tidy up the main body of my programs and eliminate some duplicated code.
namespace example_1
{
class Program
{
static void Main(string[] args)
{
int test = 5;
bool trigger = true;
if (trigger)
{
test++;
trigger = false;
}
}
}
}
namespace example_2
{
class Program
{
static void Main(string[] args)
{
int test = 5;
bool trigger = true;
if (trigger)
{
mod_test();
}
}
public static void mod_test()
{
test++;
trigger = false;
}
}
You can declare the properties outside of the methods, but still in the class :
class Program
{
// both of them are accessible within the class scope, but not outside
static int test = 5;
static bool trigger = true;
static void Main(string[] args)
{
if (trigger)
{
mod_test();
}
}
public static void mod_test()
{
test++;
trigger = false;
}
}
I think using a data container object is more suitable in this case. For example, in the following example, I wrapped the int and bool variables into a TestDataclass. This way you don't have to use global variables and still pass around the object reference for any kind of manipulation.
namespace example_3
{
class TestData
{
public bool Trigger { get; set; }
public int Test { get; set; }
}
class Program
{
static void Main(string[] args)
{
var testData = new TestData
{
Test = 5,
Trigger = true
};
if (testData.Trigger)
{
mod_test(testData);
}
}
public static void mod_test(TestData data)
{
data.Test++;
data.Trigger = false;
}
}
}

Code is compiled and run successfully but expected output is to print "Sub" is not getting printed. What is the error in this code?

What is the problem in this code?
namespace ConsoleApplication1
{
public delegate void del();
class Program
{
static void Main(string[] args)
{
del d = new del(add);
d += sub;
}
public static void add()
{
Console.WriteLine("add");
}
public static void sub()
{
Console.WriteLine("Sub");
}
}
}
You need to invoke your delegate:
class Program
{
static void Main(string[] args)
{
del d = new del(add);
d += sub;
d.Invoke();
}
public static void add()
{
Console.WriteLine("add");
}
public static void sub()
{
Console.WriteLine("Sub");
}
}
}

Call Methode in process from c#-project

I want to call a public Methode(Send) in a process from my c# project!
This is the process with the Methode that i want to call :
namespace Test123
{
class Program
{
static void Main(string[] args)
{
while(true)
{
}
}
public void Send()
{
Console.WriteLine("Test");
}
}
}
I know how to get the process but not how to invoke the methode!
I already searched on other websites and i dinĀ“t found anything that helps me.
Create a instance of the program in the static and call the method.
class Program
{
static void Main(string[] args)
{
var p = new Program();
while(true)
{
p.Send();
}
}
public void Send()
{
Console.WriteLine("Test");
}
}
If your in the same Program class, you can just call the method. The other approach listed where you declare a new Program, would be needed if you were in a different class.
static void Main(string[] args)
{
while(true)
{
Send();
}
}

Passing variables between 2 threads in a c# console application (newbie here)

I'm wondering how I can send a variable from one thread to another in a c# console application. For example,
using System;
using System.Threading;
namespace example
{
class Program
{
static void Main(string[] args)
{
int examplevariable = Convert.ToInt32(Console.ReadLine ());
Thread t = new Thread(secondthread);
t.Start();
}
static void secondthread()
{
Console.WriteLine(+examplevariable);
}
}
}
I want to make "secondthread" recognize "examplevariable".
There is an overload to Thread.Start() that takes a parameter as object. You can pass your main thread variable to that and cast it as your variable type
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int examplevariable = Convert.ToInt32(Console.ReadLine());
Thread t = new Thread(secondthread);
t.Start(examplevariable);
}
static void secondthread(object obj)
{
int examplevariable = (int) obj;
Console.WriteLine(examplevariable);
Console.Read();
}
}
}
if you want to pass multiple variable then use a model class and use property binding like below
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestModel tm = new TestModel();
tm.examplevariable1 = Convert.ToInt32(Console.ReadLine());
tm.examplevariable2 = Console.ReadLine();
Thread t = new Thread(secondthread);
t.Start(tm);
}
static void secondthread(object obj)
{
TestModel newTm = (TestModel) obj;
Console.WriteLine(newTm.examplevariable1);
Console.WriteLine(newTm.examplevariable2);
Console.Read();
}
}
class TestModel
{
public int examplevariable1 { get; set; }
public string examplevariable2 { get; set; }
}
}
Hope this will help
An easy way to do this, but might not work in all scenarios, would be to define a static variable on the class and assign the value read in from the console to the static variable. Like so:
class Program
{
static int examplevariable;
static void Main(string[] args)
{
examplevariable = Convert.ToInt32(Console.ReadLine ());
Thread t = new Thread(secondthread);
t.Start();
}
static void secondthread()
{
Console.WriteLine(+examplevariable);
}
Also, see this question on how to pass parameters to a Thread:
ThreadStart with parameters

Categories

Resources