Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Make use of generics for System.CodeDom

branch: master
README
* About this assembly

Mono.CodeDom.Generic.dll is a generic wrapper for System.CodeDom, which is
based on outdated non-generic collections.
For CodeDom compilers use, it internally uses System.CodeDom types and
supports conversions to System.CodeDom types.

* LICENSE

The entire tool is under CC0 (Public Domain Dedication):
http://creativecommons.org/publicdomain/zero/1.0/

* Design notes

The below code is a prototyping example form of the Generic CodeDom wrapper
sources. It is not precise, just a recording of its history.

---------
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.IO;
using System.Runtime.InteropServices;

using OldCodeCompileUnit = System.CodeDom.CodeCompileUnit;
using OldCodeAttributeDeclarationCollection = System.CodeDom.CodeAttributeDeclarationCollection;
using OldCodeAttributeDeclaration = System.CodeDom.CodeAttributeDeclaration;
using OldCodeTypeReference = System.CodeDom.CodeTypeReference;

namespace Mono.CodeDom.Generic
{
	public class CodeCompileUnit
	{
		OldCodeCompileUnit ccu = new OldCodeCompileUnit ();

		public CodeCompileUnit ()
		{
			AssemblyCustomAttributes = new CodeAttributeDeclarationCollection (ccu.AssemblyCustomAttributes);
			EndDirectives = new CodeDirectiveCollection (ccu.EndDirectives);
		}

		public CodeAttributeDeclarationCollection AssemblyCustomAttributes { get; private set; }

		public CodeDirectiveCollection EndDirectives { get; private set; }
	}

	public class CodeAttributeDeclarationCollection : CodeDomCollection<CodeAttributeDeclaration, OldCodeAttributeDeclaration, OldCodeAttributeDeclarationCollection>
	{
		internal CodeAttributeDeclarationCollection (OldCodeAttributeDeclarationCollection c)
			: base (c)
		{
		}

		public CodeAttributeDeclarationCollection ()
			: base ()
		{
		}

		public CodeAttributeDeclarationCollection (CodeAttributeDeclarationCollection source)
			: base (source)
		{
		}

		public CodeAttributeDeclarationCollection (params CodeAttributeDeclaration [] value)
			: base (value)
		{
		}

		internal override OldCodeAttributeDeclaration ToOld (CodeAttributeDeclaration item)
		{
			return (OldCodeAttributeDeclaration) item;
		}
	}

	public class CodeAttributeDeclaration
	{
		OldCodeAttributeDeclaration old;

		public CodeAttributeDeclaration ()
		{
			old = new OldCodeAttributeDeclaration ();
			Initialize ();
		}

		public CodeAttributeDeclaration (CodeTypeReference attributeType)
		{
			old = new OldCodeAttributeDeclaration ((OldCodeTypeReference) attributeType);
			Initialize ();
		}

		public CodeAttributeDeclaration (string name, params CodeAttributeArgument[] arguments)
		{
			old = new OldCodeAttributeDeclaration (name, arguments);
			Initialize ();
		}

		void Initialize ()
		{
			if (old.Arguments != null)
				Arguments = new CodeAttributeArgumentCollection (old.Arguments);
			if (old.AttributeType != null)
				AttributeType = new CodeTypeReference (old.AttributeType);
		}

		public CodeAttributeArgumentCollection Arguments { get; private set; }

		public CodeTypeReference AttributeType { get; private set; }

		public string Name {
			get { return old.Name; }
			set { old.Name = value; }
		}

		public static explicit operator OldCodeAttributeDeclaration (CodeAttributeDeclaration source)
		{
			return source.old;
		}
	}

	[SerializableAttribute]
	[ComVisibleAttribute (true)]
	[ClassInterfaceAttribute (ClassInterfaceType.AutoDispatch)]
	public class CodeTypeReference : CodeObject
	{
		OldCodeTypeReference old;

		public CodeTypeReference (CodeTypeParameter typeParameter)
		{
			old = new OldCodeTypeReference ((OldCodeTypeParameter) typeParameter);
			Initialize ();
		}

		internal CodeTypeReference (OldCodeTypeReference old)
		{
			this.old = old;
			Initialize ();
		}

		void Initialize ()
		{
			if (old.ArrayElementType != null)
				ArrayElementType = new CodeTypeReference (old.ArrayElementType);
			if (old.TypeArguments != null)
				TypeArguments = new CodeTypeReferenceCollection (old.TypeArguments);
		}

		public CodeTypeReference ArrayElementType { get; set; }

		public int ArrayRank {
			get { return old.ArrayRank; }
			set { old.ArrayRank = value; }
		}

		public string BaseType {
			get { return old.BaseType; }
			set { old.BaseType = value; }
		}

		[ComVisibleAttribute (false)]
		public CodeTypeReferenceOptions Options {
			get { return old.Options; }
			set { old.Options = value; }
		}

		[ComVisibleAttribute (false)]
		public CodeTypeReferenceCollection TypeArguments { get; private set; }

		public static explicit operator OldCodeTypeReference (CodeTypeReference source)
		{
			return source.old;
		}
	}

--------

	// 0: class name
	// 1: attributes
	// 2: base type name
	{1}
	public class {0} : {2}
	{
		Old{0} old;

		// constructors, copied
		public {0} (copied constructor arguments)
		{
			old = new Old{0} ((Old{0}) arguments);
			Initialize ();
		}

		// old-to-new constructor
		internal {0} (Old{0} old)
		{
			this.old = old;
			Initialize ();
		}

		// initializer
		void Initialize ()
		{
			// repeat for all auto (new-CodeDom-type) properties
			if (old.{prop} != null)
				{prop} = new {proptype}  (old.{prop});
		}

		// explicit conversion operator
		public static explicit operator Old{0} ({0} source)
		{
			return source.old;
		}

		// For a property whose type is new CodeDom type, just make it auto property (use private set for get-only ones)
		{prop-atts}
		public {CodeDomPropType} {CodeDomProp} { get; set; }

		public {NonCodeDomPropType} {NonCodeDomProp} {
			get { return old.{NonCodeDomProp}; }
			// define setter if exists.
			set { old.{NonCodeDomProp} = value; }
		}
	}
}

--------
Something went wrong with that request. Please try again.