Click here to Skip to main content
15,892,768 members
Articles / Programming Languages / C++

Practical .NET Remoting Caching Sink

Rate me:
Please Sign up or sign in to vote.
4.36/5 (12 votes)
27 Jun 20032 min read 60.3K   510   20  
A cache mechanism for .NET Remoting
using System;
using System.Runtime.Remoting.Proxies;
using System.Reflection;
using System.Collections;
using System.ComponentModel;

namespace System.Runtime.Remoting
{
	public class RemotedObjectPropertyGridWrapper : ICustomTypeDescriptor
	{
		public static object WrapIfNeeded(object o)
		{
			if(RemotingServices.IsTransparentProxy(o))return new RemotedObjectPropertyGridWrapper(o);
			else return o;
		}
		RealProxy rp;
		public RemotedObjectPropertyGridWrapper(object proxy)
		{
			if(!RemotingServices.IsTransparentProxy(proxy))
				throw new ArgumentException("not proxy","proxy");
			rp=RemotingServices.GetRealProxy(proxy);
		}
		AttributeCollection ICustomTypeDescriptor.GetAttributes()
		{
			return TypeDescriptor.GetAttributes(this, true);
		}

		string ICustomTypeDescriptor.GetClassName()
		{
			return TypeDescriptor.GetClassName(this, true);
		}

		string ICustomTypeDescriptor.GetComponentName()
		{
			return TypeDescriptor.GetComponentName(this, true);
		}

		TypeConverter ICustomTypeDescriptor.GetConverter()
		{
			return TypeDescriptor.GetConverter(this, true);
		}

		EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
		{
			return TypeDescriptor.GetDefaultEvent(this, true);
		}

		PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
		{
			return null;
		}
		object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
		{
			return TypeDescriptor.GetEditor(this, editorBaseType, true);
		}

		EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
		{
			return TypeDescriptor.GetEvents(this, true);
		}

		EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
		{
			return TypeDescriptor.GetEvents(this, attributes, true);
		}

		/*AttributeCollection ICustomTypeDescriptor.GetAttributes()
		{
			return TypeDescriptor.GetAttributes(rp.GetProxiedType());
		}

		string ICustomTypeDescriptor.GetClassName()
		{
			return rp.GetProxiedType().Name;
		}

		string ICustomTypeDescriptor.GetComponentName()
		{
			return TypeDescriptor.GetComponentName(this, true);
		}
		
		TypeConverter ICustomTypeDescriptor.GetConverter()
		{
			return TypeDescriptor.GetConverter(rp.GetProxiedType());
		}

		EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
		{
			return TypeDescriptor.GetDefaultEvent(rp.GetProxiedType());
		}

		PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
		{
			return TypeDescriptor.GetDefaultProperty(rp.GetProxiedType());
		}

		object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
		{
			return TypeDescriptor.GetEditor(rp.GetProxiedType(), editorBaseType, true);
		}

		EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
		{
			return TypeDescriptor.GetEvents(rp.GetProxiedType());
		}

		EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
		{
			return TypeDescriptor.GetEvents(rp.GetProxiedType(), attributes);
		}*/

		PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
		{
			return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
		}

		PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
		{
			ArrayList a=new ArrayList();
			foreach(PropertyInfo p in rp.GetProxiedType().GetProperties())
			{
				ArrayList attrs = new ArrayList();
				attrs.AddRange(p.GetCustomAttributes(true));
				if(!p.CanWrite)
					attrs.Add(new ReadOnlyAttribute(true));

				a.Add(new RemotedPropertyDescriptor(rp.GetTransparentProxy(),p,
					(Attribute[])attrs.ToArray(typeof(Attribute))));
			}
			return new PropertyDescriptorCollection(a.ToArray(typeof(PropertyDescriptor)) as PropertyDescriptor[]) ;
		}

		object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
		{
			return this;//rp.GetTransparentProxy();
		}
		private class RemotedPropertyDescriptor : PropertyDescriptor
		{
			object obj;
			PropertyInfo pinfo;
			public RemotedPropertyDescriptor(object obj, PropertyInfo pinfo, Attribute[] attrs) :
				base(pinfo.Name, attrs)
			{
				this.obj=obj;
				this.pinfo=pinfo;
			}

			public override Type ComponentType
			{
				get { return obj.GetType(); }
			}

			public override bool IsReadOnly
			{
				get { return (Attributes.Matches(ReadOnlyAttribute.Yes)); }
			}

			public override Type PropertyType
			{
				get { return pinfo.PropertyType; }
			}

			public override bool CanResetValue(object component)
			{
				return false;				
			}

			public override object GetValue(object component)
			{
				return RemotedObjectPropertyGridWrapper.WrapIfNeeded(pinfo.GetValue(obj,null));
			}

			public override void ResetValue(object component)
			{				
			}

			public override void SetValue(object component, object value)
			{
				pinfo.SetValue(obj,value,null);
			}

			public override bool ShouldSerializeValue(object component)
			{
				return false;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
zhi
Researcher
United States United States
My name is Zhiheng Cao. I major in Eletronics Engineering, in University of Tokyo. I do part time jobs of computer programming and teaching high school students English and Mathematics.

CodeProject provided me with valuable information for my programming job many times in the past. I hope I can do my part by providing goodies I have.


Comments and Discussions