Getting class and method names at runtime - c#

How to get the name of the class and the name of the method of this class at runtime. The code gets compiled and then obfuscated using some open source obfuscator. Here is an example:
class MainClass {
public static void Main(string[] args) {
Console.WriteLine(nameof(Test));
Console.WriteLine(nameof(Test.TestMethod));
Console.ReadLine();
}
}
class Test {
public static void TestMethod() {
Console.WriteLine("Hello World!");
}
}
The obfuscator renames classes and methods like this:
MainClass -> A
MainClass.Main -> A.a
Test -> B
Test.TestMethod -> B.a
When I run my code after compilation and obfuscation I get:
B
TestMethod
So the nameof works as expected for the class name, but doesn't work for the method name. How does the nameof work? What is the correct way to get the obfuscated names of the class and the method at runtime?

Use the following:
class MainClass {
public static void Main(string[] args) {
var methodinfo = typeof(Test).GetMethod("TestMethod");
var handle = methodinfo.MetaDataToken;
MethodBase method = System.Reflection.MethodBase.GetMethodFromHandle(handle);
string methodName = method.Name;
string className = method.ReflectedType.Name;
string fullMethodName = className + "." + methodName;
Console.WriteLine(fullMethodName);
Console.ReadLine();
}
}
class Test {
public static void TestMethod() {
Console.WriteLine("Hello World!");
}
}

Related

Unable to call extension method during runtime for dynamic variable

Below is the sample code and it is throwing error (''string' does not contain a definition for 'ToUpperExtn'') during runtime. How can i handle this.
class Program
{
static void Main(string[] args)
{
dynamic name = "Prasad";
**Console.WriteLine(Convert.ToString(name).ToUpperExtn());** // Error: ''string' does not contain a definition for 'ToUpperExtn''
string name1 = "Prasad";
Console.WriteLine(name1.ToUpperExtn()); // Working fine
}
}
public static class StringExtensions
{
public static string ToUpperExtn(this string value)
{
return value.ToUpper();
}
}

How to pass parameters from main method to another class

How do I pass the parameters from main method to another class?
I have done like this. Is this right approach?
namespace classA
{
class Program
{
public static void Main(string[] args)
{
string abc= new string {"HELLO"};
Console.WriteLine("My result: {0}", ClassB(abc));
Console.Read();
}
public static string ClassB(string abc)
{
//code
return xyz;
}
}
}
well in the code you posted you have only one class with two methods but in principle yes, this would be one way:
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string abc = "HELLO";
Console.WriteLine("My result: {0}", ClassB(abc));
Console.Read();
}
public static string ClassB(string abc)
{
return abc;
}
}
}
My result: HELLO
If you have your second method really in another STATIC class:
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string abc = "HELLO";
Console.WriteLine("My result: {0}", myfuncclass.ClassB(abc));
Console.Read();
}
}
static class myfuncclass
{
public static string ClassB(string abc)
{
return abc;
}
}
}
My result: HELLO
And if your second method is NOT in a static class and your method is NOT static you have to create an instance of the class first and then call your method on this object:
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string abc = "HELLO";
myfuncclass test = new myfuncclass();
Console.WriteLine("My result: {0}", test.ClassB(abc));
Console.Read();
}
}
public class myfuncclass
{
public string ClassB(string abc)
{
return abc;
}
}
}

How to use an interface within a public interface with a main?

//Program.cs
public interface TestVal
{
//Input Param
string Input { get; }
//will return output
TestValRes ValidateRe(string input);
}
class MyClass : ITestVal
{
static void Main(string[] args)
{
var instance = new MyClass();
instance.Run();
}
public void Run()
{
ValidateRe("test");
}
public ITestValRes ValidateRe(string input)
{
return null; // return an instance of a class implementing ITestValRes here.
}
}
//TestvalRes.cs
public interface TestvalRes
{
string Input { get; }
bool IsValid { get; }
}
So I just want to pass a string to the TestVal, do validation and call TestvalRes to return whether it is Valid or not, and if Invalid, why? So the validation will be done in the first public interface - TestVal, however I still need to call it inside the Main(), right?
First off, I'd recommend following C# naming conventions and name your interfaces ITestVal and ITestValRes respectively.
Next, static method cannot call instance methods in the same class (without creating an instance and using that). You need to create an instance of the class and pass control of the application flow to that:
class MyClass : ITestVal
{
static void Main(string[] args)
{
var instance = new MyClass();
instance.Run();
}
public void Run()
{
ValidateRe("test");
}
public ITestValRes ValidateRe(string input)
{
return null; // return an instance of a class implementing ITestValRes here.
}
}

How to get delegate name inside delegated method?

How to get delegate name inside delegated method?
Here is my program for testing:
namespace Test
{
class Program
{
public Action action;
void real()
{
// I hoped it would output "action" here, but it was "real"
Console.WriteLine(MethodInfo.GetCurrentMethod().Name);
}
public Program()
{
action = real;
}
static void Main(string[] args)
{
Program pr = new Program();
pr.action();
}
}
}
So how can I get the name of delegate action instead of method read?
I've tried MethodInfo.GetCurrentMethod(), but it didn't work.
Consider
static void Main(string[] args)
{
Program pr = new Program();
Action tempName1 = pr.action;
Action tempName2 = tempName1;
//pr.action();
tempName2();
}
Which name would you like to get? tempName1, tempName2, pr.action or just action?
From these choices it follows that you can't get an unambiguous variable name.

while overidding base methods, why default arguments are not working well? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# optional parameters on overridden methods
using System;
namespace Apple
{
class A
{
public virtual void Func(int a=4){
Console.WriteLine(" A Class: "+a);
}
}
class B : A
{
public override void Func(int a = 12)
{
Console.WriteLine(" B Class: "+ a);
}
}
public class Program
{
static void Main(string[] args)
{
A ob = new B();
ob.Func();
Console.ReadLine();
}
}
}
// Output: B Class: 4
Default parameters are filled at compile time, and the code references a variable ob through the base class. The virtual method override works at run time, as expected. You could achieve the desired effect by using method overload:
class A
{
public void Func(int value)
{
}
public virtual void Func()
{
Func(4);
}
}
class B: A
{
public override void Func()
{
Func(12);
}
}
The compiler places the the default parameter value based on the type of the object and is done during the compile time.
Hence the compiled code would look like:
using System;
namespace Apple
{
public class Program
{
private static void Main(string[] args)
{
A ob = new B();
ob.Func(4);
Console.ReadLine();
}
}
}
You could get the desired result by doing this:
public class Program
{
static void Main(string[] args)
{
A ob = new B();
((B)ob).Func();
Console.ReadLine();
}
}
Because you creating instance of Class A which is referring to the address of class B.
A ob = new B();
Since the instance is of class A, the method you calling is pointing to method in class A.
You can check this by putting debug and then execute the code.
instead if you create instance of class B ie
B ob = new B();
it will call the method Fun() from class B and will display output as
" B Class: 12"
the default parameter value is Static binding.

Categories