Not sure if this qualifies as "advanced", but I'm having trouble decorating in a derived class events/properties of the inherited class with additional attributes. I've included some code to show what I mean - I want to do this without shadowing. So, in this example I want DeviceB to be able to add attributes to an event from DeviceA. So, if it's not something obvious, are there any workarounds or best practices for this situation?
Thanks. Brian ---8<---8<---8<---8<---8<---8<---8<---8<-- using System; using System.Reflection; namespace AttributeTest { public class Program { static void Main(string[] args) { DeviceB device = new DeviceB(); Type deviceType = device.GetType(); Console.WriteLine("Checking class:" + device.GetType().Name); foreach (MemberInfo member in deviceType.GetMembers()) { if (Attribute.IsDefined(member, typeof(DeviceBindingAttribute), true)) { Console.WriteLine("Class member has binding: " + member.Name); // get all attributes, in-case multiple are defined. // Specifying to search this member's inheritance chain to find the attributes. object[] attributes = member.GetCustomAttributes(typeof(DeviceBindingAttribute), true); foreach (DeviceBindingAttribute bindingAttribute in attributes) { Object panel = Activator.CreateInstance(bindingAttribute.PanelType); if (member.MemberType == MemberTypes.Event) { EventInfo eventInfo = deviceType.GetEvent(member.Name); Delegate d = Delegate.CreateDelegate(eventInfo.EventHandlerType, panel, bindingAttribute.BindToName); eventInfo.AddEventHandler(device, d); Console.WriteLine("Attached delegate to " + bindingAttribute.BindToName); } //else if (member.MemberType == MemberTypes.Property) //{ // object retVal = deviceType.InvokeMember(member.Name, BindingFlags.GetProperty, null, device, new object[] { }); // panel.GetType().InvokeMember(bindingAttribute.BindToName, BindingFlags.SetProperty, null, panel, new object[] { retVal }); //} } } } Console.ReadLine(); } } #region attribute class [AttributeUsage(AttributeTargets.Event | AttributeTargets.Property, AllowMultiple = true)] public sealed class DeviceBindingAttribute : Attribute { private Type panelType; private string bindToName; public DeviceBindingAttribute(Type panelType, string bindToName) { this.panelType = panelType; this.bindToName = bindToName; } public Type PanelType { get { return panelType; } } public string BindToName { get { return bindToName; } } } #endregion public class StatusPanel { public void UpdateStatus(object sender, EventArgs e){} } public class PositionPanel { public void UpdateStatusForPosition(object sender, EventArgs e) { } } public class DeviceA { [DeviceBindingAttribute(typeof(StatusPanel), "UpdateStatus")] public virtual event EventHandler<EventArgs> DeviceStatusChanged; } public class DeviceB : DeviceA { // This binds only this attribute: [DeviceBindingAttribute(typeof(PositionPanel), "UpdateStatusForPosition")] public override event EventHandler<EventArgs> DeviceStatusChanged { add { base.DeviceStatusChanged += value; } remove { base.DeviceStatusChanged -= value; } } // This binds both events: //[DeviceBindingAttribute(typeof(StatusPanel), "UpdateStatus")] //[DeviceBindingAttribute(typeof(PositionPanel), "UpdateStatusForPosition")] //public new event EventHandler<EventArgs> DeviceStatusChanged; } } =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com