Call function from Main() - c#

I am new to C# and I'm having a little problem with calling a function from the Main() method.
class Program
{
static void Main(string[] args)
{
test();
}
public void test()
{
MethodInfo mi = this.GetType().GetMethod("test2");
mi.Invoke(this, null);
}
public void test2()
{
Console.WriteLine("Test2");
}
}
I get a compiler error in test();:
An object reference is required for the non-static field.
I don't quite understand these modifiers yet so what am I doing wrong?
What I really want to do is have the test() code inside Main() but it gives me an error when I do that.

If you still want to have test() as an instance method:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.test();
}
void Test()
{
// I'm NOT static
// I belong to an instance of the 'Program' class
// You must have an instance to call me
}
}
or rather make it static:
class Program
{
static void Main(string[] args)
{
Test();
}
static void Test()
{
// I'm static
// You can call me from another static method
}
}
To get the info of a static method:
typeof(Program).GetMethod("Test", BindingFlags.Static);

Just put all logic to another class
class Class1
{
public void test()
{
MethodInfo mi = this.GetType().GetMethod("test2");
mi.Invoke(this, null);
}
public void test2()
{
Console.Out.WriteLine("Test2");
}
}
and
static void Main(string[] args)
{
var class1 = new Class1();
class1.test();
}

The method must be static in order to call it.

Related

How can be call the method from another class inherited

How can be call the method from another class inherited
let's see my code please:
class Program : classA
{
static void Main(string[] args)
{
// how i can call method ToDo without create an instance like below
//classA c = new classA();
//c.ToDo();
Console.ReadLine();
}
}
class Program2 : classB
{
static void Main(string[] args)
{
// how i can call method ToDo
//ToDo()
Console.ReadLine();
}
}
public abstract class classB
{
public void ToDo()
{
Console.WriteLine("classB");
}
}
public class classA
{
public void ToDo()
{
Console.WriteLine("classA");
}
}
how i can call the method in Either way, please help me.
There are a couple ways to do what you want to do (they're kind of similar or even the same).
One way is to create a class with a static method:
public class classA
{
public static void ToDo()
{
Console.WriteLine("classA");
}
}
then call it like:
classA.ToDo();
Another way is to add another static method to the class that contains Main:
class Program2 : classB
{
static void Main(string[] args)
{
ToDo()
Console.ReadLine();
}
static void Todo()
{
// do stuff here
}
}
If u want to call ToDo() function into [class Program : classA] and [class Program : classB]
Without creating Instance.
U have to Define ToDo() function as static, then u can call this method with class name in anywhere.
public static void ToDo(){}

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");
}
}
}

Async&Await IL Code

If i implement a default async/await method like:
class Program
{
static void Main(string[] args)
{
new Program().TestMethod();
}
private async void TestMethod()
{
await Task.Delay(10);
}
}
The generated IL-Code and the async state machine contains a field "this" with a reference to the instance which invoked the method. If the code is adjusted to a static one, there is of course no "this" value.
Not to do this via reflection, more from perspective of the CLR or the debugger.
Does anyone know how i can determine within the "static TestMethod" from which method i was called - on the IL level!
class Program
{
static void Main(string[] args)
{
TestMethod();
}
private static async void TestMethod()
{
await Task.Delay(10);
}
}
how i can determine within the "static TestMethod" from which method i was called
You can use CallerMemberName attribute
static void Main(string[] args)
{
new Program().TestMethod();
Console.ReadLine();
}
private async void TestMethod([CallerMemberName]string caller = "")
{
Console.WriteLine(caller);
}
PS: You can also use CallerFilePath or CallerLineNumber
using System;
using System.Diagnostics;
namespace Cli
{
class Program
{
static void Main(string[] args)
{
new Program().TestMethod();
}
private void TestMethod()
{
var sf = new StackFrame(1); //get caller stackframe
var mi = sf.GetMethod(); //get method info of caller
Console.WriteLine("{0}::{1}", mi.DeclaringType, mi.Name);
// Cli.Program::Main
}
}
}

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();
}
}

How to call a class when a class within a Static

Hello friends I face this question in one Interview in statics(Myclass). I have two clases one is M1 and M2.
How can I call m2 in m1 and m1 in m2? and also How to create an Instance of an static?
static void Main(string[] args)
{
//in this Portion How can I call all member if MyClass
}
public static class Myclass
{
public static class M1
{
//Here How can I call m2
}
public class m2
{
//Here How can I call m1
}
}
For static class M1, you need to call using class name.
For non static class M2, you need to create instance.
you can call methods/properties, and not classes.
public static class Myclass
{
public static void Main(String[] args)
{
// call something on m2
var m2 = new Myclass.m2();
m2.A2(); // call m2.A2 method
// call something on m1
Myclass.m1.A1();
}
public static class M1
{
//Here How can I call m2
public static void A1()
{
var m2 = new Myclass.m2(); // create m2instance
m2.A2(); // call m2
}
}
public class m2
{
//Here How can I call m1
public void A2()
{
Myclass.M1.A1(); // called M1.A1
}
}
}
You can not call a class, and as it is static you can not Generate new class of that type but you can call methods and functions of it easily like this:
public static class Myclass
{
public static class M1
{
public static class Method2(){
}
new m2().AMethod();
}
public class m2
{
//Here How can I call m1
public static void AMethod(){
//method
}
}
}
And for main:
static void Main(string[] args)
{
new MyClass.m2().AMethod();
MyClass.M1.Method2();
}

Categories

Resources