Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#

Commonly Used .NET Coding Patterns in CodeDom

Rate me:
Please Sign up or sign in to vote.
4.80/5 (51 votes)
31 Oct 200613 min read 180.5K   738   269  
A library of CodeDom templates of commonly used coding practices in .NET.
using System;
using System.CodeDom;
using System.Reflection;

namespace DotNetZen.CodeDom
{
	internal class Utils
	{
		/*private Utils()
		{
		}

		public static void ImplementInterfaceImplicit(CodeTypeDeclaration type, System.Type @interface)
		{
			foreach (CodeTypeMember member in ImplementInterfaceCore(@interface))
			{
				if (member is CodeMemberEvent)
					((CodeMemberEvent)(member)).PrivateImplementationType = new CodeTypeReference(@interface);
				else if (member is CodeMemberMethod)
					((CodeMemberMethod)(member)).PrivateImplementationType = new CodeTypeReference(@interface);
				else if (member is CodeMemberProperty)
					((CodeMemberProperty)(member)).PrivateImplementationType = new CodeTypeReference(@interface);

				type.Members.Add(member);
			}
		}

		public static void ImplementInterfaceExplicit(CodeTypeDeclaration type, System.Type @interface)
		{
			foreach (CodeTypeMember member in ImplementInterfaceCore(@interface))
			{
				if (member is CodeMemberEvent)
					((CodeMemberEvent)(member)).ImplementationTypes.Add(@interface);
				else if (member is CodeMemberMethod)
					((CodeMemberMethod)(member)).ImplementationTypes.Add(@interface);
				else if (member is CodeMemberProperty)
					((CodeMemberProperty)(member)).ImplementationTypes.Add(@interface);

				member.Attributes &= ~MemberAttributes.ScopeMask;
				member.Attributes |= MemberAttributes.Public;

				type.Members.Add(member);
			}
		}

		private static MemberAttributes GetMemberAttributes(MethodAttributes methodAttribs)
		{
			MemberAttributes attribs;
			attribs &= MemberAttributes
		}

		private static CodeMemberMethod CreateCodeMemberMethod(MethodInfo methodInfo)
		{
			CodeMemberMethod method = new CodeMemberMethod();
			method.Attributes = GetMemberAttributes(methodInfo.Attributes);
			method.Name = methodInfo.Name;
			
			foreach (ParameterInfo parameter in methodInfo.GetParameters())
			{
				method.Parameters.Add(CloneCodeParameterDeclarationExpression(parameter));
			}

			method.ReturnType = new CodeTypeReference(methodInfo.ReturnType);

			return method;
		}

		private static CodeMemberEvent CreateCodeMemberEvent(EventInfo eventInfo)
		{
			/*CodeMemberEvent @event = new CodeMemberEvent();
			@event.Attributes = GetMemberAttributes(eventInfo.GetAddMethod(true).Attributes);
			@event.Name = eventInfo.Name;
			@event.Type = new CodeTypeReference(eventInfo.EventHandlerType);

			return @event;*//*
			throw new NotImplementedException();
		}

		private static CodeMemberProperty CreateCodeMemberProperty(PropertyInfo propertyInfo)
		{
			throw new NotImplementedException();
		}

		private static CodeTypeMemberCollection ImplementInterfaceCore(System.Type @interface)
		{
			if (!@interface.IsInterface)
				throw new InvalidOperationException("Type not an interface.");

			CodeTypeMemberCollection collection = new CodeTypeMemberCollection();

			foreach (MethodInfo decl in @interface.GetMethods())
			{
				collection.Add(CreateCodeMemberMethod(decl));
			}

			/*foreach (EventInfo decl in @interface.GetEvents())
			{
				collection.Add(CreateCodeMemberEvent(decl));
			}

			foreach (PropertyInfo decl in @interface.GetProperties())
			{
				collection.Add(CreateCodeMemberProperty(decl));
			}*//*

			return collection;
		}*/
	}
}

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
Web Developer
Israel Israel
Omer van Kloeten is a senior .NET consultant and a lecturer at the Sela Group.

My blogs:
English: http://weblogs.asp.net/OKloeten/
English and Hebrew: http://blogs.microsoft.co.il/blogs/omer

You can find more details about the Sela Group here:
http://www.sela.co.il

Comments and Discussions