473,473 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2146

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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.