473,382 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Overriding properties with Reflection.Emit

ey all,
I'm in the process of creating a method of
tracking whenever an objects properties are changed. Since
its not possible to alter

existing types I decided to used a proxy generated using
Reflection.Emit which inherits from the object needed

Person p = (Person)ProxyBuilder.Construct(typeof(Person));

this proxy builder overrides the properties of Person and
adds in the nessecary code to trigger the property changed
event.

This is the code to generate the proxy

public static Type Construct(Type type)
{
AssemblyName name = new AssemblyName();
name.Name = "ObjectServerProxies";

AssemblyBuilder assembly =
AppDomain.CurrentDomain.DefineDynamicAssembly(name ,
AssemblyBuilderAccess.Run);

ModuleBuilder module = assembly.DefineDynamicModule
("Proxies");

TypeBuilder proxy = module.DefineType(type.Name
+ "Proxy", TypeAttributes.Public |
TypeAttributes.AutoClass |

TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit,
type);

ConstructorBuilder constructor =
proxy.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard, new Type[0]);

ILGenerator constructorIL =
constructor.GetILGenerator();
ConstructorInfo baseConstructor =
type.GetConstructor(new Type[0]);
constructorIL.Emit(OpCodes.Ldarg_0);
constructorIL.Emit(OpCodes.Call, baseConstructor);
constructorIL.Emit(OpCodes.Ret);

foreach(PropertyInfo propertyInfo in
type.GetProperties())
{
PropertyBuilder property =
proxy.DefineProperty(propertyInfo.Name,
propertyInfo.Attributes, propertyInfo.PropertyType, null);

MethodAttributes attributes =
MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.SpecialName |

MethodAttributes.Virtual;

MethodBuilder getMethod =
proxy.DefineMethod("get_" + propertyInfo.Name, attributes,
propertyInfo.PropertyType, null);
ILGenerator getIL =
getMethod.GetILGenerator();
getIL.Emit(OpCodes.Ldarg_0);
getIL.Emit(OpCodes.Call,
propertyInfo.GetGetMethod());
getIL.Emit(OpCodes.Ret);

MethodBuilder setMethod =
proxy.DefineMethod("set_" + propertyInfo.Name, attributes,
typeof(void), new

Type[]{propertyInfo.PropertyType});
ILGenerator setIL =
setMethod.GetILGenerator();
setIL.Emit(OpCodes.Ldarg_0);
setIL.Emit(OpCodes.Ldarg_1);
setIL.Emit(OpCodes.Call,
propertyInfo.GetSetMethod());
setIL.Emit(OpCodes.Ldarg_0);
setIL.Emit(OpCodes.Ldstr,
propertyInfo.Name);
setIL.Emit(OpCodes.Newobj, typeof
(PropertyChangedEventArgs));

MethodInfo onPropertyChanged = typeof
(MappedObject).GetMethod("OnPropertyChanged",
BindingFlags.NonPublic |

BindingFlags.Instance);

setIL.Emit(OpCodes.Call,
onPropertyChanged);
setIL.Emit(OpCodes.Ret);

property.SetGetMethod(getMethod);
property.SetSetMethod(setMethod);

}

return proxy.CreateType();
}

Now the problem I'm having is that the properties are not
being overriden, if I use reflection to iterate though the
proxys properties each

property is showing up twice, one with the declaring type
Person and the other with declaring type PersonProxy,
rather than just the

PersonProxy property being displayed.

Any help appreciated.

Thanks
Nigel Sampson
Jul 21 '05 #1
0 2132

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. ...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
3
by: John Arthur | last post by:
Hi, I am reading about Reflection.Emit and I can't really imagine a real situation where I can use this. Can someone give me an example that can't be done without Reflection or an example of...
3
by: ayende | last post by:
How do I use Reflection.Emit to produce code similar to this? public void bar<T>() { } public void foo<J>() { bar<J>(); }
0
by: Nigel Sampson | last post by:
ey all, I'm in the process of creating a method of tracking whenever an objects properties are changed. Since its not possible to alter existing types I decided to used a proxy generated using...
3
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
5
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
9
by: Michael Sander | last post by:
Hello ng, anyone knowns how to add a reference to an assembly to System.Reflection.AssemblyBuilder? In System.Web.Compilation.AssemblyBuilder is a function like AddAssemblyReference, but not in...
4
by: Neil Chambers | last post by:
I would like to pass an array of strings to an object and have the object members built using the array items as property names. How would I go about doing this? Example: public class...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.