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();
}
Related
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];
}
}
}
I need to make a cast with a type passed into a variaable, this is possible ? if yes, how to do this ?
Inside of ForEach action I need to make the cast, I don't have the type, but just a instance of object....
for example: ( of course this is just a hypothetical example to show what I'm trying to do )
private static void ForEach(Type[] tp, Action<string, Type> action)
{
for (int i = 0; i < tp.Length; i++)
action("test", tp[i]);
}
static void Main()
{
Type[] tp = new Type[]
{
typeof(int),
typeof(string),
typeof(double)
};
ForEach(tp, (x, y) =>
{
// make the cast here
// var aaaa = (y)(x);
});
}
static T Cast<T>(object input)
{
return (T)input;
}
I have a Dictionary:
Dictionary<int, Type> AllDrillTypes = new Dictionary<int, Type>()
{
{13,typeof(TCHEMISTRY)},
{14,typeof(TDRILLSPAN)}
};
where TCHEMISTRY and TDRILLSPAN are classes. Then I want to get rows from one of this classes like this:
Type T = AllDrillTypes[13];
var LC = Activator.CreateInstance( typeof(List<>).MakeGenericType( T ) );
MethodInfo M = T.GetMethod("FindAll", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);
LC = M.Invoke(null, new object[] { });
All this code works correctly. After that I need to get some rows like this:
var LingLC = from obj in LC where obj.RunID == 1001 select obj;
But this line causes error:
"Could not find an implementation of the query pattern for source type
'object'. 'Where' not found."
What's wrong with this code line?
Even if you can't change the class definitions, you can avoid using reflection:
// Getter dictionary rather than type dictionary.
Dictionary<int, Func<IEnumerable<object>>> DrillTypeGetters =
new Dictionary<int, Func<IEnumerable<object>>>()
{
{ 13, () => TCHEMISTRY.FindAll().Cast<object>() },
{ 14, () => TDRILLSPAN.FindAll().Cast<object>() }
};
Dictionary<int, Func<object, int>> IDGetters =
new Dictionary<int, Func<object, int>>()
{
{ 13, o => ((TCHEMISTRY)o).RunID },
{ 14, o => ((TDRILLSPAN)o).RunID }
};
IEnumerable<object> LC = DrillTypeGetters[13]();
IEnumerable<object> LingLC =
from obj in LC
where IDGetters[13](obj) == 1001
select obj;
Or you could even just switch on 13/14 and run a completely different method per type.
if (choice == 13)
IEnumerable<TCHEMISTRY> LingLC =
TCHEMISTRY.FindAll().Where(tc => tc.RunID == 1001);
else if (choice == 14)
IEnumerable<TDRILLSPAN> LingLC =
TDRILLSPAN.FindAll().Where(td => td.RunID == 1001);
Basically, if the two classes don't share any common hierarchy, you can't write any common code to deal with them. If they have lots of similar properties, you can use getters as in my first example to provide a way to get the similar properties whatever type of class you're dealing with. If they don't even have similar properties, don't try to write shared code.
Maybe you could rewrite your code to something like this, .... to get a more type-safe solution (without using reflection).
void Main()
{
var driller1 = new DrillerWhichYouCannotChange1();
var driller2 = new DrillerWhichYouCannotChange2();
var allDrillTypes = new Dictionary<int, IList<IDriller>>()
{
{ 13, new List<IDriller>() { new DrillerWhichYouCannotChange1Adapter(driller1) } },
{ 14, new List<IDriller>() { new DrillerWhichYouCannotChange2Adapter(driller2) } },
};
Console.WriteLine(allDrillTypes[13][0].SomeCommonProperty); // prints 123
Console.WriteLine(allDrillTypes[14][0].SomeCommonProperty); // prints 456
}
interface IDriller
{
int SomeCommonProperty { get; }
}
class DrillerWhichYouCannotChange1Adapter : IDriller
{
private DrillerWhichYouCannotChange1 inner;
public DrillerWhichYouCannotChange1Adapter(DrillerWhichYouCannotChange1 inner)
{
this.inner = inner;
}
public int SomeCommonProperty { get { return this.inner.PropertyX; } }
}
class DrillerWhichYouCannotChange2Adapter : IDriller
{
private DrillerWhichYouCannotChange2 inner;
public DrillerWhichYouCannotChange2Adapter(DrillerWhichYouCannotChange2 inner)
{
this.inner = inner;
}
public int SomeCommonProperty { get { return this.inner.PropertyY; } }
}
class DrillerWhichYouCannotChange1
{
public int PropertyX { get { return 123; } }
}
class DrillerWhichYouCannotChange2
{
public int PropertyY { get { return 456; } }
}
EDIT: If you cannot change the driller classes, you could use the adapter-pattern to create one adapter per driller, which implements IDiller.
you need to cast LC to the to the return type of the FindAll method. Something on the lines of:
var genericList = ((List<TChemistry>) LC);
var LingLC = from obj in genericList where obj.RunID == 1001 select obj;
This is assuming that FindAll returns a collection of TChemistry.
--edit
If you do not know at runtime if the type will be TChemistry or TDrillspan, then you will have to write an if/else of switch statement to cast to the correct type. I would rather have TChemistry and TDrillSpan extend an abstract class or an interface, and you can just cast to List, and you will always have RunId property.
public abstract class TAbstract
{
public abstract int RunId {get; set;}
}
public class TChemistry : TAbstract
{
public override int RunId {get; set;}
}
public class TDrillSpan : TAbstract
{
public override int RunId {get; set;}
}
Type T = AllDrillTypes[13] as TAbstract;
var LC = Activator.CreateInstance( typeof(List<>).MakeGenericType( T ) );
MethodInfo M = T.GetMethod("FindAll", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);
LC = M.Invoke(null, new object[] { });
var genericList = ((List<TAbstract>) LC);
var LingLC = from obj in genericList where obj.RunID == 1001 select obj;
If you cannot change the declaration of classes, then you are only left with ugly if else:
var typeInfo = LC.GetType();
IEnumerable<T> genericList;
if (typeInfo == typeof(IEnumerable<TChemistry>)
{
genericList = (List<TChemistry>) LC;
)
else if (typeInfo == typeof(IEnumerable<TDrillSpan>)
{
genericList = (List<TDrillSpan>) LC;
}
var LingLC = from obj in genericList where obj.RunID == 1001 select obj;
try
IEnumerable returnedObjects = (IEnumerable)M.Invoke(null, new object[] { }) as IEnumerable;
then iterate through your ienumerable
foreach (object report in returnedObjects)
{
// Use reflection to read properties or add to a new List<object> if you
// need an ICollection<object>
}
instead of :
LC = M.Invoke(null, new object[] { });
using System;
using System.Reflection;
namespace Reflection
{
class Test
{
protected void methodname(int i)
{
Console.WriteLine(("in the world of the reflection- only i"));
Console.Read();
}
protected void methodname(int i, int j)
{
Console.WriteLine(("in the world of the reflection i , j"));
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
// BindingFlags eFlags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public|BindingFlags.NonPublic;
BindingFlags eFlags = BindingFlags.Instance|BindingFlags.NonPublic;
Test aTest = new Test();
MethodInfo mInfoMethod = typeof(Reflection.Test).GetMethod("methodname", eFlags);
mInfoMethod.Invoke(aTest, new object[] { 10 ,20});
}
}
}
I want to call both Getmethod() overloaded methods. If i give the method name , an runtime error is thrown(ambigous method call) . How to avoid this and how each method can be called.
You have to pass types of your overloaded method, this is how reflection sorts out your desired method when there's a overload.
You can't call both the methods as it has different types of input parameter. You have to know exactly which one you exactly want to call, and pass along a Type[], for instance:
// invoking overload with two parameters
MethodInfo mInfoMethod =
typeof(Reflection.Test).GetMethod(
"methodname",
BindingFlags.Instance | BindingFlags.NonPublic,
Type.DefaultBinder,
new[] {typeof (int), typeof (int)},
null);
mInfoMethod.Invoke(aTest, new object[] { 10 ,20});
OR
// invoking overload with one parameters
MethodInfo mInfoMethod =
typeof(Reflection.Test).GetMethod(
"methodname",
vBindingFlags.Instance | BindingFlags.NonPublic,
Type.DefaultBinder,
new[] { typeof (int) },
null);
mInfoMethod.Invoke(aTest, new object[] { 10 });
Use 'GetMethods' instead to retrieve all the overloads, then pick the ones you want.
Please find a working sample below:
public class ReflectionSample
{
protected void Method(int i)
{
Console.WriteLine(string.Format("in the world of the reflection- only {0}", i));
Console.Read();
}
protected void Method(int i, int j)
{
Console.WriteLine(string.Format("in the world of the reflection {0} , {1}", i,j));
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
var eFlags = BindingFlags.Instance | BindingFlags.NonPublic;
var objType = Type.GetType("Sample.ReflectionSample");
var methods = objType.GetMethods(eFlags);
foreach (var method in methods)
{
if (method.Name == "Method")
{
Console.WriteLine("Method name is :" + method.Name);
var parameters = method.GetParameters();
int value = 10;
List<object> param = new List<object>();
for (int i = 0; i < parameters.Count(); i++)
{
param.Add(value * 5);
}
Console.WriteLine(parameters.Count());
method.Invoke(new ReflectionSample(), param.ToArray());
}
}
}
}
can u try like this
You have to specify which method you want:
class SomeType
{
void Foo(int size, string bar) { }
void Foo() { }
}
SomeType obj = new SomeType();
// call with int and string arguments
obj.GetType().GetMethod("Foo", new Type[] { typeof(int), typeof(string)).Invoke(obj, new object[] { 42, "Hello" });
// call without arguments
obj.GetType().GetMethod("Foo", new Type[0]).Invoke(obj, new object[0]);
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.