ILGenerator Emit : Load propertyInfo has method parameter - c#

I'm trying to make this code in IL using ILGenerator.Emit
class Foo
{
...
}
public class TestClass
{
public static void test()
{
Type t = typeof(Foo);
foreach(PropertyInfo p in t.GetProperties())
{
GenerateVar(p.PropertyInfo);
}
}
public static object GenerateVar(Type var)
{
if (var == typeof(Int32))
{
return 5;
}
else if (var == typeof(Char))
{
return 'a';
}
return null;
}
}
This is what I've done so far, and had some complications:
MethodInfo mi = TestClass.GetType().GetMethod("GenerateVar",
BindingFlags.Public |
BindingFlags.Instance);
ILGenerator generator = mb.GetILGenerator();
LocalBuilder propertyType;
LocalBuilder TestClass = mb_gen.DeclareLocal(typeof(TestClass));
foreach (PropertyInfo pi in t.GetProperties())
{
propertyType = mb_gen.DeclareLocal(pi.PropertyType);
//loads into the stack the current PropertyType and the method class
generator.Emit(OpCodes.Ldloc, TestClass);
generator.Emit(OpCodes.LdLoc, propertyType);
//calls GenerateVar(Type var) to get a PropertyType var
generator.Emit(OpCodes.Callvirt, mi);
}
It gives me the following exception:
-> expected type: System.Type , Received type: System.String
System.String is the property type that was given by: pi.PropertyType;
What am I doing wrong?
Thanks in advance

As thehennyy comment, if you can give us the full code we will can to help better. I'm trying to help here because I guess what you try to do.
So I based here on your C# code. As I understand you want to create a method that get properties of type (Foo in your case) and foreach of them, get some value based on type.
Here is a snippet of doing it for first property of the type. To complete the code you need to emit the loop, or, like you wrote in your question, loop on the properties in your C# code and emit the code for each property one after one.
static void CallGenerateVar()
{
var dm = new DynamicMethod("CallGenerateVar", typeof(object), Type.EmptyTypes, typeof(TestClass));
MethodInfo generateVarMethod = typeof(TestClass).GetMethod("GenerateVar", BindingFlags.Public | BindingFlags.Instance);
var ilGen = dm.GetILGenerator();
var properties = ilGen.DeclareLocal(typeof(PropertyInfo[]));
var index = ilGen.DeclareLocal(typeof(int));
var propInfo = ilGen.DeclareLocal(typeof(PropertyInfo));
ilGen.Emit(System.Reflection.Emit.OpCodes.Ldtoken, typeof(Foo));
ilGen.Emit(System.Reflection.Emit.OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public));
ilGen.Emit(System.Reflection.Emit.OpCodes.Callvirt, typeof(Type).GetMethod("GetProperties", Type.EmptyTypes));
ilGen.Emit(System.Reflection.Emit.OpCodes.Stloc_0);
ilGen.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);
ilGen.Emit(System.Reflection.Emit.OpCodes.Stloc_1);
ilGen.Emit(System.Reflection.Emit.OpCodes.Ldloc_0);
ilGen.Emit(System.Reflection.Emit.OpCodes.Ldloc_1);
ilGen.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
ilGen.Emit(System.Reflection.Emit.OpCodes.Stloc_2);
ilGen.Emit(System.Reflection.Emit.OpCodes.Ldloc_0);
ilGen.Emit(System.Reflection.Emit.OpCodes.Ldloc_2);
ilGen.Emit(System.Reflection.Emit.OpCodes.Callvirt, typeof(PropertyInfo).GetMethod("get_PropertyType", BindingFlags.Instance | BindingFlags.Public));
ilGen.Emit(System.Reflection.Emit.OpCodes.Callvirt, generateVarMethod);
ilGen.Emit(System.Reflection.Emit.OpCodes.Ret);
var del = (Func<object>)dm.CreateDelegate(typeof(Func<object>));
var result = del.Invoke();
}
In case our Foo type looks like this:
class Foo
{
public int MyProperty { get; set; }
}
And GenerateVar looks like this:
public object GenerateVar(Type var)
{
if (var == typeof(Int32))
{
return 5;
}
else if (var == typeof(Char))
{
return 'a';
}
return null;
}
It will print 5

Related

Create an instance of class by Reflection.Emit

I'm trying to create this C# class by using System.Reflection.Emit.
private class MyTestData : IMyClonable
{
private int _testValue = 0;
public int testValue
{
get { return _testValue; }
set { _testValue = value; }
}
public IMyClonable Clone()
{
MyTestData clone = new MyTestData();
clone._testValue = _testValue;
return clone ;
}
}
This class must be created from this interface:
public interface IMyTestData : IMyClonable
{
int testValue { get; set; }
}
I already made the code that generate properties and this work's fine. But I stuck when I'm trying to create method Clone(). I don't know how to create an instance of this class itself and save it in a local variable.
Here is the code to generate method Clone():
private static void MakeCloneMethod(Type componentType, TypeBuilder typeBuilder)
{
ConstructorBuilder ctor =
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
MethodInfo cloneMethod = typeof(IMyClonable).GetMethod("Clone");
MethodAttributes methodIlAttributes =
(cloneMethod.Attributes & ~MethodAttributes.Abstract) | MethodAttributes.Final;
MethodBuilder cloneMthdBldr = typeBuilder.DefineMethod(
"Clone", methodIlAttributes, typeof(IMyClonable), new Type[] { });
ILGenerator ilgen = cloneMthdBldr.GetILGenerator();
LocalBuilder returnValue = ilgen.DeclareLocal(typeBuilder.AsType());
ilgen.Emit(OpCodes.Newobj, ctor);
ilgen.Emit(OpCodes.Stloc_S, returnValue);
CloneProperties(componentType, ilgen);
ilgen.Emit(OpCodes.Ldloc_S);
ilgen.Emit(OpCodes.Ret);
typeBuilder.DefineMethodOverride(cloneMthdBldr, cloneMethod);
}
private static void CloneProperties(Type componentType, ILGenerator ilgen)
{
PropertyInfo[] allProperties = GetPublicProperties(componentType);
foreach (PropertyInfo propInfo in allProperties)
{
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldfld, builders[propInfo]);
ilgen.Emit(OpCodes.Stfld, builders[propInfo]);
ilgen.Emit(OpCodes.Ldloc_0);
}
}
I get the System.InvalidProgramException when I try to call Clone() method. Even if I comment call of method CloneProperties(). What I'm doing wrong?
I got it! Here is working code:
private static void MakeCloneMethod(Type componentType, TypeBuilder typeBuilder)
{
Type thisType = typeBuilder.AsType();
Type itype = typeof(IMyClonable);
MethodInfo cloneMthd = itype.GetMethod("Clone");
MethodBuilder cloneMthdBldr = typeBuilder.DefineMethod(
cloneMthd.Name, cloneMthd.Attributes & ~MethodAttributes.Abstract,
itype, new Type[] {});
typeBuilder.DefineMethodOverride(cloneMthdBldr, cloneMthd);
ILGenerator gen = cloneMthdBldr.GetILGenerator();
LocalBuilder returnValue = gen.DeclareLocal(thisType);
gen.Emit(OpCodes.Newobj, typeBuilder.DefineDefaultConstructor(MethodAttributes.Public));
gen.Emit(OpCodes.Stloc_S, returnValue);
PropertyInfo[] allProperties = GetPublicProperties(componentType);
foreach (PropertyInfo propInfo in allProperties)
{
gen.Emit(OpCodes.Ldloc_S, returnValue);
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldfld, builders[propInfo]);
gen.Emit(OpCodes.Stfld, builders[propInfo]);
}
gen.Emit(OpCodes.Ldloc_S, returnValue);
gen.Emit(OpCodes.Ret);
}
Thanks to Svick!

Reflection.Emit Implement interface and create array IL

Im trying to create a Proxy Class from an interface. In the methods I just want to collect all arguments in an object array and pass on to a known method. So faar I have managed to get it working without params and with return types. As soon as I try to create my array I get "Additional information: Common Language Runtime detected an invalid program.".. Dont really know how to debug from here and the IL codes seems to be correct ( ? ).
public class Program
{
static void Main(string[] args)
{
var v = CreateProxy<IFoo>();
v.DoSomething();
}
public static void TheMethod(object[] args)
{
}
public interface IFoo
{
void DoSomething();
}
public static T CreateProxy<T>()
{
var interfaceType = typeof(T);
AssemblyName assemblyName = new AssemblyName(string.Format("tmp_{0}", interfaceType.FullName));
string moduleName = string.Format("{0}.dll", assemblyName.Name);
string ns = interfaceType.Namespace;
if (!string.IsNullOrEmpty(ns))
ns += ".";
var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,AssemblyBuilderAccess.RunAndSave);
var module = assembly.DefineDynamicModule(moduleName, false);
var type = module.DefineType(String.Format("{0}Proxy_{1}", ns, interfaceType.Name), TypeAttributes.Class | TypeAttributes.AnsiClass |TypeAttributes.Sealed |TypeAttributes.NotPublic);
type.AddInterfaceImplementation(interfaceType);
//Constructor
var ctor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new Type[] {});
var generator = ctor.GetILGenerator();
generator.Emit(OpCodes.Ret);
//Methods
foreach (var method in interfaceType.GetMethods())
{
var args = method.GetParameters();
var methodImpl = type.DefineMethod(method.Name, MethodAttributes.Public | MethodAttributes.Virtual, method.ReturnType, (from arg in args select arg.ParameterType).ToArray());
generator = methodImpl.GetILGenerator();
generator.Emit(OpCodes.Nop);
generator.Emit(OpCodes.Ldc_I4_1);
generator.Emit(OpCodes.Newarr, typeof(object));
generator.Emit(OpCodes.Stloc_0);
generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Call, typeof(Program).GetMethod(nameof(Program.TheMethod)));
generator.Emit(OpCodes.Nop);
generator.Emit(OpCodes.Ret);
}
return (T)Activator.CreateInstance(type.CreateType());
}
}
The method im trying to Emit should look like this.
public void DoSomething()
{
object[] arr = new object[1];
Program.TheMethod(arr);
}
What am I missing here ?
You should initialize locals:
foreach (var method in interfaceType.GetMethods())
{
var args = method.GetParameters();
var methodImpl = type.DefineMethod(method.Name, MethodAttributes.Public | MethodAttributes.Virtual, method.ReturnType, (from arg in args select arg.ParameterType).ToArray());
generator = methodImpl.GetILGenerator();
generator.DeclareLocal(typeof(object[]));
....
....

Make Func<> for a generic type

I have this architecture.
public void Init()
{
PropertyInfo[] infos = typeof(Transform).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo info in infos)
{
// Save getter
MethodInfo method = info.GetGetMethod();
System.Type returnType = method.ReturnType;
System.Func<Transform, Vector3> fact = GetFactory<Transform, Vector3>(method);
Vector3 v = fact(this.Value);
Debug.Log("Test123 " + v);
//_getters.Add(info.Name, newMethod);
}
}
static System.Func<T, T1> GetFactory<T, T1>(MethodInfo info)
{
return (System.Func<T, T1>)GetFactory(typeof(T), typeof(T1), info);
}
static object GetFactory(System.Type type, System.Type type1, MethodInfo info)
{
System.Type funcType = typeof(System.Func<,>).MakeGenericType(type, type1);
return System.Delegate.CreateDelegate(funcType, info);
}
It even works if method.ReturnType is Vector3.
But I want the func<Transform, Vector3> to be func<Transform, ReturnType>.
I have no idea doing this.
Does someone of you know how I can do this?
And I also have no idea how to save the result in a dictionary.
Which type can the dictionary be of?
public Dictionary<string, System.Func<object, object>> _getters = new Dictionary<string, System.Func<object, object>>();
Edit: No ideas?
From what I get from the comment, is that you want to access getters through a string key? If that is the case, you might want to use the code sample below.
The entity you want to access:
class Entity
{
public int Foo { get { return 42; } }
public string Bar { get { return "Magic"; } }
}
The class that allows you to access the properties by name:
class PropertyCaller<T>
{
// Static for each type T
private static readonly IDictionary<string, Func<T, object>> _propertyLookup;
static PropertyCaller()
{
_propertyLookup = new Dictionary<string, Func<T, object>>();
Type objectType = typeof (T);
foreach (PropertyInfo prop in objectType.GetProperties())
{
const BindingFlags num = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
MethodInfo getMethod = prop.GetGetMethod(true);
_propertyLookup.Add(prop.Name, item => getMethod.Invoke(item, num, null, null, null));
}
}
public static object Call(T obj, string propertyName)
{
Func<T, object> f;
if (!_propertyLookup.TryGetValue(propertyName, out f))
{
throw new InvalidOperationException();
}
return f(obj);
}
}
Example usage:
Entity e = new Entity();
var a = PropertyCaller<Entity>.Call(e, "Foo"); // 42
var b = PropertyCaller<Entity>.Call(e, "Bar"); // Magic

How do I use reflection to get properties explicitly implementing an interface?

More specifically, if I have:
public class TempClass : TempInterface
{
int TempInterface.TempProperty
{
get;
set;
}
int TempInterface.TempProperty2
{
get;
set;
}
public int TempProperty
{
get;
set;
}
}
public interface TempInterface
{
int TempProperty
{
get;
set;
}
int TempProperty2
{
get;
set;
}
}
How do I use reflection to get all the propertyInfos for properties explicitly implementing TempInterface?
Thanks.
I think the class you are looking for is System.Reflection.InterfaceMapping.
Type ifaceType = typeof(TempInterface);
Type tempType = typeof(TempClass);
InterfaceMapping map = tempType.GetInterfaceMap(ifaceType);
for (int i = 0; i < map.InterfaceMethods.Length; i++)
{
MethodInfo ifaceMethod = map.InterfaceMethods[i];
MethodInfo targetMethod = map.TargetMethods[i];
Debug.WriteLine(String.Format("{0} maps to {1}", ifaceMethod, targetMethod));
}
The property getter and setter of an explicitly implemented interface property has an unusual attribute. It's IsFinal property is True, even when it is not a member of a sealed class. Try this code to verify my assertion:
foreach (AssemblyName name in Assembly.GetEntryAssembly().GetReferencedAssemblies()) {
Assembly asm = Assembly.Load(name);
foreach (Type t in asm.GetTypes()) {
if (t.IsAbstract) continue;
foreach (MethodInfo mi in t.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)) {
int dot = mi.Name.LastIndexOf('.');
string s = mi.Name.Substring(dot + 1);
if (!s.StartsWith("get_") && !s.StartsWith("set_")) continue;
if (mi.IsFinal)
Console.WriteLine(mi.Name);
}
}
}
Here's a modified solution based on the implementation given in this blog post:
var explicitProperties =
from prop in typeof(TempClass).GetProperties(
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
let getAccessor = prop.GetGetMethod(true)
where getAccessor.IsFinal && getAccessor.IsPrivate
select prop;
foreach (var p in explicitProperties)
Console.WriteLine(p.Name);
Building on the answer by MrKurt:
var targetMethods =
from iface in typeof(TempClass).GetInterfaces()
from method in typeof(TempClass).GetInterfaceMap(iface).TargetMethods
select method;
var explicitProps =
from prop in typeof(TempClass).GetProperties(BindingFlags.Instance |
BindingFlags.NonPublic)
where targetMethods.Contains(prop.GetGetMethod(true)) ||
targetMethods.Contains(prop.GetSetMethod(true))
select prop;
I had to modify Jacob Carpenter's answer but it works nicely. nobugz's also works but Jacobs is more compact.
var explicitProperties =
from method in typeof(TempClass).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
where method.IsFinal && method.IsPrivate
select method;
A simple helper class that could help:
public class InterfacesPropertiesMap
{
private readonly Dictionary<Type, PropertyInfo[]> map;
public InterfacesPropertiesMap(Type type)
{
this.Interfaces = type.GetInterfaces();
var properties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
this.map = new Dictionary<Type, PropertyInfo[]>(this.Interfaces.Length);
foreach (var intr in this.Interfaces)
{
var interfaceMap = type.GetInterfaceMap(intr);
this.map.Add(intr, properties.Where(p => interfaceMap.TargetMethods
.Any(t => t == p.GetGetMethod(true) ||
t == p.GetSetMethod(true)))
.Distinct().ToArray());
}
}
public Type[] Interfaces { get; private set; }
public PropertyInfo[] this[Type interfaceType]
{
get { return this.map[interfaceType]; }
}
}
You'll get properties for each interface, even explicitly implemented.
It's overly complex. You have to reflect over the methods/properties of the Interface type, see if they exist in your class type, and compare them to see if they're the "same" when they do exist.
If something is in the interface but not the type you're testing, it's an explicit implementation. If it's in both, but different between the two, it's an explicit interface.
This seems a bit painful for no apparent reason!
My solution is for the case where you know the name of the property you are looking for and is pretty simple.
I have a class for making reflection a bit easier that I just had to add this case to:
public class PropertyInfoWrapper
{
private readonly object _parent;
private readonly PropertyInfo _property;
public PropertyInfoWrapper(object parent, string propertyToChange)
{
var type = parent.GetType();
var privateProperties= type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance);
var property = type.GetProperty(propertyToChange) ??
privateProperties.FirstOrDefault(p => UnQualifiedNameFor(p) == propertyName);
if (property == null)
throw new Exception(string.Format("cant find property |{0}|", propertyToChange));
_parent = parent;
_property = property;
}
private static string UnQualifiedNameFor(PropertyInfo p)
{
return p.Name.Split('.').Last();
}
public object Value
{
get { return _property.GetValue(_parent, null); }
set { _property.SetValue(_parent, value, null); }
}
}
You cant just do == on name because explicitly implemented properties have fully qualified names.
GetProperties needs both the search flags to get at private properties.
Jacob's code is missing a filter:
var props = typeof(TempClass).GetInterfaces().Where(i => i.Name=="TempInterface").SelectMany(i => i.GetProperties());
foreach (var prop in props)
Console.WriteLine(prop);

GetProperties() to return all properties for an interface inheritance hierarchy

Assuming the following hypothetical inheritance hierarchy:
public interface IA
{
int ID { get; set; }
}
public interface IB : IA
{
string Name { get; set; }
}
Using reflection and making the following call:
typeof(IB).GetProperties(BindingFlags.Public | BindingFlags.Instance)
will only yield the properties of interface IB, which is "Name".
If we were to do a similar test on the following code,
public abstract class A
{
public int ID { get; set; }
}
public class B : A
{
public string Name { get; set; }
}
the call typeof(B).GetProperties(BindingFlags.Public | BindingFlags.Instance) will return an array of PropertyInfo objects for "ID" and "Name".
Is there an easy way to find all the properties in the inheritance hierarchy for interfaces as in the first example?
I've tweaked #Marc Gravel's example code into a useful extension method encapsulates both classes and interfaces. It also add's the interface properties first which I believe is the expected behaviour.
public static PropertyInfo[] GetPublicProperties(this Type type)
{
if (type.IsInterface)
{
var propertyInfos = new List<PropertyInfo>();
var considered = new List<Type>();
var queue = new Queue<Type>();
considered.Add(type);
queue.Enqueue(type);
while (queue.Count > 0)
{
var subType = queue.Dequeue();
foreach (var subInterface in subType.GetInterfaces())
{
if (considered.Contains(subInterface)) continue;
considered.Add(subInterface);
queue.Enqueue(subInterface);
}
var typeProperties = subType.GetProperties(
BindingFlags.FlattenHierarchy
| BindingFlags.Public
| BindingFlags.Instance);
var newPropertyInfos = typeProperties
.Where(x => !propertyInfos.Contains(x));
propertyInfos.InsertRange(0, newPropertyInfos);
}
return propertyInfos.ToArray();
}
return type.GetProperties(BindingFlags.FlattenHierarchy
| BindingFlags.Public | BindingFlags.Instance);
}
Type.GetInterfaces returns the flattened hierarchy, so there is no need for a recursive descent.
The entire method can be written much more concisely using LINQ:
public static IEnumerable<PropertyInfo> GetPublicProperties(this Type type)
{
if (!type.IsInterface)
return type.GetProperties();
return (new Type[] { type })
.Concat(type.GetInterfaces())
.SelectMany(i => i.GetProperties());
}
Interface hierarchies are a pain - they don't really "inherit" as such, since you can have multiple "parents" (for want of a better term).
"Flattening" (again, not quite the right term) the hierarchy might involve checking for all the interfaces that the interface implements and working from there...
interface ILow { void Low();}
interface IFoo : ILow { void Foo();}
interface IBar { void Bar();}
interface ITest : IFoo, IBar { void Test();}
static class Program
{
static void Main()
{
List<Type> considered = new List<Type>();
Queue<Type> queue = new Queue<Type>();
considered.Add(typeof(ITest));
queue.Enqueue(typeof(ITest));
while (queue.Count > 0)
{
Type type = queue.Dequeue();
Console.WriteLine("Considering " + type.Name);
foreach (Type tmp in type.GetInterfaces())
{
if (!considered.Contains(tmp))
{
considered.Add(tmp);
queue.Enqueue(tmp);
}
}
foreach (var member in type.GetMembers())
{
Console.WriteLine(member.Name);
}
}
}
}
Exactly the same problem has a workaround described here.
FlattenHierarchy doesnt work btw. (only on static vars. says so in intellisense)
Workaround. Beware of duplicates.
PropertyInfo[] pis = typeof(IB).GetProperties(BindingFlags.Public | BindingFlags.Instance);
Type[] tt = typeof(IB).GetInterfaces();
PropertyInfo[] pis2 = tt[0].GetProperties(BindingFlags.Public | BindingFlags.Instance);
Responding to #douglas and #user3524983, the following should answer the OP's question:
static public IEnumerable<PropertyInfo> GetPropertiesAndInterfaceProperties(this Type type, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance)
{
if (!type.IsInterface) {
return type.GetProperties( bindingAttr);
}
return type.GetInterfaces().Union(new Type[] { type }).SelectMany(i => i.GetProperties(bindingAttr)).Distinct();
}
or, for an individual property:
static public PropertyInfo GetPropertyOrInterfaceProperty(this Type type, string propertyName, BindingFlags bindingAttr = BindingFlags.Public|BindingFlags.Instance)
{
if (!type.IsInterface) {
return type.GetProperty(propertyName, bindingAttr);
}
return type.GetInterfaces().Union(new Type[] { type }).Select(i => i.GetProperty( propertyName, bindingAttr)).Distinct().Where(propertyInfo => propertyInfo != null).Single();
}
OK next time I'll debug it before posting instead of after :-)
this worked nicely and tersely for me in a custom MVC model binder. Should be able to extrapolate to any reflection scenario though. Still kind of stinks that it's too pass
var props = bindingContext.ModelType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).ToList();
bindingContext.ModelType.GetInterfaces()
.ToList()
.ForEach(i => props.AddRange(i.GetProperties()));
foreach (var property in props)

Categories