Accessing the constructor by using Reflection - c#

Assume the class is public and and the constructor is internal like as
Public class A
{
private string text;
internal A(string submittedText);
public string StrText { get; }
}
In this case how could I Access the constructor by using Reflection. What I have done so far
Type[] pTypes = new Type[1];
pTypes[0] = typeof(object);
object[] argList = new object[1];
argList[0] = "Some Text";
ConstructorInfo c = typeof(A).GetConstructor
(BindingFlags.NonPublic |
BindingFlags.Instance,
null,
pTypes,
null);
A foo = (A)c.Invoke(BindingFlags.NonPublic,
null,
argList,
Application.CurrentCulture);
But it shows an error. Any Suggestions

I think the error might casued by the GetConstructor, you passed in Object type instead of String type.
var ctr = typeof(A).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(String) }, null);
btw, if the type A itself is internal, and you know public Type B and A in the same assembly, you can try:
Type typeA = typeof(B).Assembly.GetType("Namespace.AssemblyName.A", false);
var ctr = typeA.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(String) }, null);

Try this:
Type type = typeof(A);
Type[] argTypes = new Type[] { typeof(String) };
ConstructorInfo cInfo = type.GetConstructor(argTypes);
object[] argVals = new object[] { "Some string" };
Ap = (A)cInfo.Invoke(argVals);
I got help from this site:
http://www.java2s.com/Code/CSharp/Reflection/CallGetConstructortogettheconstructor.htm
I just tried it on a sample console app, where I had an internal class and it worked.
namespace ConsoleApplication1
{
internal class Person
{
public Person(string name)
{
Name = name;
}
public string Name { get; set; }
}
}
public static void Main()
{
Type type = typeof(Person);
Type[] argTypes = new Type[] { typeof(String) };
ConstructorInfo cInfo = type.GetConstructor(argTypes);
object[] argVals = new object[] { "Some string" };
Person p = (Person)cInfo.Invoke(argVals);
}

Argument type in the constructor is string, not object. So maybe like this:
pTypes[0] = typeof(string);

You should be using Activator.CreateInstance.

you can use object o1 = Activator.CreateInstance(typeof (myclass), true); for creating a instance. no need to go through that complicated code for creating instances in the same method.

Related

why reflection GetMethod return null?

I need to create an instance of object dynamically and execute one method of this instance dynamically. I am trying this code but GetMethod return null.
var className = "SomeClass";
Type[] paramTypes = { typeof(Telegram.Bot.Types.User), typeof(string[]) };
var cmd = Activator.CreateInstance(null, "mynamespace." + className);
var method = cmd.GetType().GetMethod("Execute", BindingFlags.Public|BindingFlags.Instance, null, paramTypes, null);
res = method.Invoke(cmd, new object[] { e.Message.From, args }).ToString();
and this is my SomeClass code:
public class RegisterTelegramCommand : ITelegramCommand
{
public string Message
{
get
{
return "some message";
}
}
public string Execute(Telegram.Bot.Types.User telegramUser, string[] param)
{
return param[0]+" " +param[2];
}
}
how can i solve this problem?
Activator.CreateInstance returns a ObjectHandle which needs to be unwrapped first:
var className = "RegisterTelegramCommand";
Type[] paramTypes = { typeof(object), typeof(string[]) };
var cmd = Activator.CreateInstance("ConsoleApplication4", "ConsoleApplication4." + className);
Object p = cmd.Unwrap();
var method = p.GetType().GetMethod("Execute", BindingFlags.Public | BindingFlags.Instance, null, paramTypes, null);
var res = method.Invoke(p, new object[] { null, args }).ToString();
I have putted parameter null, might be due that this issue is comming, I have check this in console code is working fine
using System;
using System.Reflection;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
var className = "RegisterTelegramCommand";
Type[] paramTypes = { typeof(object), typeof(string[]) };
var cmd = Activator.CreateInstance("ConsoleApplication4", "ConsoleApplication4." + className);
Object p = cmd.Unwrap();
var method = p.GetType().GetMethod("Execute", BindingFlags.Public | BindingFlags.Instance, null, paramTypes, null);
var res = method.Invoke(p, new object[] { null, args }).ToString();
Console.Read();
}
}
public class RegisterTelegramCommand
{
public string Message
{
get { return "a"; }
}
public string Execute(object paramObject, string[] param)
{
return param[0] + " " + param[2];
}
}
}

Reflection.Emit with generic type = type is not generic

I am using Refletion.Emit, I have an interface, an abstract class and another class. What I want to achieve is create a new class based on those two.
So here is the simple interface:
public interface IHello()
{
string SayHello();
}
This is my abstract class:
public abstract class Helloer<T> where T : IHello, new()
{
private readonly string text;
protected Helloer(string text)
{
this.text = text;
}
public string DoIt()
{
var t = new T();
return t.SayHello() + text;
}
}
and the second class:
public class Howdy : IHello
{
public string SayHello() { return "Howdy"; }
}
Now this is the full main code responsible for creating the new type HowdyHelloer:
public static void Run()
{
var type = CreateHelloer(typeof(Howdy));
dynamic helloer = Activator.CreateInstance(type);
Console.WriteLine(helloer.DoIt());
}
public static Type CreateHelloer(Type hello)
{
var assemblyBuilder = GetAssemblyBuilder("MyAssembly");
var moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");
var typeBuilder = moduleBuilder.DefineType(hello.Name + "Helloer", TypeAttributes.Public);
var parentType = typeof(Helloer<>).MakeGenericType(hello);
typeBuilder.SetParent(parentType);
Type[] types = new Type[1];
types[0] = typeof(string);
var parentCtorGeneric1 = typeof(Helloer<>).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, types, null);
var parentCtor = TypeBuilder.GetConstructor(parentType, parentCtorGeneric1);
var ctor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { });
var ctorIl = ctor.GetILGenerator();
ctorIl.Emit(OpCodes.Ldstr, "Partner");
ctorIl.Emit(OpCodes.Call, parentCtor);
ctorIl.Emit(OpCodes.Ret);
return typeBuilder.CreateType();
}
public static AssemblyBuilder GetAssemblyBuilder(string name)
{
var assemblyName = new AssemblyName(name);
var domain = AppDomain.CurrentDomain;
AssemblyBuilder c = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
return c;
}
On the line:
var parentCtor = TypeBuilder.GetConstructor(parentType, parentCtorGeneric1);
I'm getting an error: " 'type' must contain a TypeBuilder as a generic argument."
Can anyone help me with this please? As I'm trying to solve this for last 3 days and nothing :/ I did research and to be honest I didn't found anything specific on using Emit with generic abstract classes.
i see in your code at least two error
first:
var parentCtor = TypeBuilder.GetConstructor(parentType, parentCtorGeneric1);
here parentType was not created with TypeBuilder, so if you want get parent constuctor just get it from parent type like
var parentCtorGeneric1 = parentType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, types, null);
second: you wrong create consctuctor code, it should like this
var ctorIl = ctor.GetILGenerator();
ctorIl.Emit(OpCodes.Ldarg_0); // show where to load the following string
ctorIl.Emit(OpCodes.Ldstr, "Partner");
ctorIl.Emit(OpCodes.Call, parentCtorGeneric1);
ctorIl.Emit(OpCodes.Ret);

Creating an object of Generic Class with dynamically generated type

I have a generic Class
public class MyClass<T>
{
public List<T> Translate(List<T> list, params string[] names)
{
//do something here,modify list and return list
}
}
Now i can easily create its instances like
MyClass<Employee> obj= new MyClass<Employee>(); OR
MyClass<Vehicle> obj = new MyClass<Vehicle>();
I can call my method like
obj.Translate(Mylist of employee or vehicle type,"param1","param2")
But in my case i dont know the type T as its generated on runtime,see the code below
String classname = string.Empty;
if(Classtype == 1)
{
classname = "Employee"
}
else if(classtype == 2)
{
classname = "Vehicle"
}
I want something like below..so that i can creat an instance of this generic class
MyClass<typeof(classname)> empp = new MyClass<typeof(classname)>();
empp.Translate(MyList,"param1","param2")
Please suggest,how can i do that.
Try
var someType = Type.GetType("MyProject.Employee");
var yourGenericType = typeof(MyClass<>).MakeGenericType(new [] { someType });
var instance = Activator.CreateInstance(yourGenericType);
Note that Type.GetType(...) only works with the full namespace of a type, or even the full assembly qualified name if your class is not in the same dll as the code that is executing this.
Thanks All for the Help I was able to do it by this way
var someType = Type.GetType("MyProject.Employee");
var genericType = typeof(Convert<>).MakeGenericType(new[] { someType });
var instance = Activator.CreateInstance(genericType);
string instanceType = instance.GetType().FullName;
MethodInfo methodInfo = Type.GetType(instanceType).GetMethod("Translate");
genericType.InvokeMember(methodInfo.Name, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, instance, new object[] { lstEmp, "Name", "City" });
ishan
public T CreateEmptyObject<T>()
{
return (T)Activator.CreateInstance(typeof(T));;
}
To call it:
YourClassObject yourClassObject = CreateEmptyObject<YourClassObject>();

How to create an instance of the class is created at run time

i write some method for create class and property in run time with Reflection.Emit
my code is:
public class DynamicLibraryProperties
{
public string PropName { get; set; }
public Type PropType { get; set; }
public string DefaultValue { get; set; }
}
public class GenerateDynamicClass
{
public static void GenerateLegacyStructureObject(string libraryName, string className, List<DynamicLibraryProperties> properties)
{
ILGenerator ilgen = default(ILGenerator);
string library = string.Concat(libraryName, ".dll");
AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(libraryName), AssemblyBuilderAccess.RunAndSave);
ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule(libraryName, library);
TypeBuilder legacyBuilder = modBuilder.DefineType(string.Concat(libraryName, ".", className), TypeAttributes.Class | TypeAttributes.Public);
//Field Builder - Based on number of months add so many fields
foreach (DynamicLibraryProperties p in properties)
{
FieldBuilder field = legacyBuilder.DefineField(string.Concat("_", p.PropName), p.PropType, FieldAttributes.Private);
PropertyBuilder nameProp = legacyBuilder.DefineProperty(p.PropName, PropertyAttributes.HasDefault, p.PropType, null);
Type[] types = new Type[] { p.PropType };
dynamic typeConvertor = TypeDescriptor.GetConverter(p.PropType);
dynamic defaultValue = typeConvertor.ConvertFromString(p.DefaultValue);
ConstructorInfo ctor = typeof(DefaultValueAttribute).GetConstructor(types);
CustomAttributeBuilder customAttrib = new CustomAttributeBuilder(ctor, new object[] { defaultValue });
nameProp.SetCustomAttribute(customAttrib);
MethodAttributes getAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName;
MethodBuilder getNameBuilder = legacyBuilder.DefineMethod(string.Concat("get_", p.PropName), getAttr, p.PropType, Type.EmptyTypes);
ilgen = getNameBuilder.GetILGenerator(); ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldfld, field);
ilgen.Emit(OpCodes.Ret);
MethodBuilder setNameBuilder = legacyBuilder.DefineMethod(string.Concat("set_", p.PropName), getAttr, null, new Type[] { p.PropType });
ilgen = setNameBuilder.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldarg_1);
ilgen.Emit(OpCodes.Stfld, field); ilgen.Emit(OpCodes.Ret);
nameProp.SetGetMethod(getNameBuilder);
nameProp.SetSetMethod(setNameBuilder);
}
Type objType = Type.GetType("System.Object");
ConstructorInfo objCtor = objType.GetConstructor(Type.EmptyTypes);
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Call, objCtor);
ilgen.Emit(OpCodes.Ret);
legacyBuilder.CreateType();
asmBuilder.Save(library);
}
}
and use this code like this
List<DynamicLibraryProperties> props = new List<DynamicLibraryProperties>();
props.Add(new DynamicLibraryProperties
{
PropName = "201203", PropType = typeof(float),DefaultValue = "0"
});
props.Add(new DynamicLibraryProperties { PropName = "201204", PropType = typeof(float) ,DefaultValue = "0"});
GenerateDynamicClass.GenerateLegacyStructureObject("test", "test", props);
Now i want create instance of Test class and set value for property value but i don't no how to do it, please help me , thanks all.
You can use Activator.CreateInstance for this, for that you need Type of your test class.
Change the method as below.
public static Type GenerateLegacyStructureObject(string libraryName, string className, List<DynamicLibraryProperties> properties)
{
//your code
Type t = legacyBuilder.CreateType();
asmBuilder.Save(library);
return t;
}
Then use it like this
Type testType = GenerateDynamicClass.GenerateLegacyStructureObject("test", "test", props);
object test = Activator.CreateInstance(testType);
Hope this helps

Reflection to invoke the constructor with ConstructorInfo

In a very Simple class like below,
class Program
{
public Program(int a, int b, int c)
{
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
}
and I use reflection to invoke the constructor
something like this...
var constructorInfo = typeof(Program).GetConstructor(new[] { typeof(int), typeof(int), typeof(int) });
object[] lobject = new object[] { };
int one = 1;
int two = 2;
int three = 3;
lobject[0] = one;
lobject[1] = two;
lobject[2] = three;
if (constructorInfo != null)
{
constructorInfo.Invoke(constructorInfo, lobject.ToArray);
}
But I am getting an error saying "object does not match target type constructor info".
any help/comments greatly appreciated.
thanks in advance.
You don't need to pass constructorInfo as a parameter, as soon as you are calling a constructor, but not an instance method of an object.
var constructorInfo = typeof(Program).GetConstructor(
new[] { typeof(int), typeof(int), typeof(int) });
if (constructorInfo != null)
{
object[] lobject = new object[] { 1, 2, 3 };
constructorInfo.Invoke(lobject);
}
For KeyValuePair<T,U>:
public Program(KeyValuePair<int, string> p)
{
Console.WriteLine(string.Format("{0}:\t{1}", p.Key, p.Value));
}
static void Main(string[] args)
{
var constructorInfo = typeof(Program).GetConstructor(
new[] { typeof(KeyValuePair<int, string>) });
if (constructorInfo != null)
{
constructorInfo.Invoke(
new object[] {
new KeyValuePair<int, string>(1, "value for key 1") });
}
Console.ReadLine();
}

Categories