Table of Contents

Class CodeDomHelper

Namespace
NMF.CodeGen
Assembly
NMF.CodeGen.dll

A helper class to generate code using CodeDOM

public static class CodeDomHelper
Inheritance
CodeDomHelper
Inherited Members

Methods

AddAttribute(CodeTypeMember, Type, params object[])

Adds an attribute of the given attribute type to the given code member

public static void AddAttribute(this CodeTypeMember member, Type attributeType, params object[] arguments)

Parameters

member CodeTypeMember

The code member

attributeType Type

The type of the attribute

arguments object[]

The arguments of the attribute, can either be primitive values, code expressions or code attribute arguments

ApplyExpression<T1, TResult>(Expression<Func<T1, TResult>>, CodeExpression)

Applies the given expression to the given code expressions as input

public static CodeExpression ApplyExpression<T1, TResult>(Expression<Func<T1, TResult>> expression, CodeExpression argument)

Parameters

expression Expression<Func<T1, TResult>>

The expression to be converted to CodeDOM

argument CodeExpression

The first argument

Returns

CodeExpression

The CodeDOM expression that contains the equivalent code

Type Parameters

T1

The type of the first argument

TResult

The type of the result

ApplyExpression<T1, T2, TResult>(Expression<Func<T1, T2, TResult>>, CodeExpression, CodeExpression)

Applies the given expression to the given code expressions as input

public static CodeExpression ApplyExpression<T1, T2, TResult>(Expression<Func<T1, T2, TResult>> expression, CodeExpression argument1, CodeExpression argument2)

Parameters

expression Expression<Func<T1, T2, TResult>>

The expression to be converted to CodeDOM

argument1 CodeExpression

The first argument

argument2 CodeExpression

The second argument

Returns

CodeExpression

The CodeDOM expression that contains the equivalent code

Type Parameters

T1

The type of the first argument

T2

The type of the second argument

TResult

The type of the result

ApplyExpression<T1, T2, T3, TResult>(Expression<Func<T1, T2, T3, TResult>>, CodeExpression, CodeExpression, CodeExpression)

Applies the given expression to the given code expressions as input

public static CodeExpression ApplyExpression<T1, T2, T3, TResult>(Expression<Func<T1, T2, T3, TResult>> expression, CodeExpression argument1, CodeExpression argument2, CodeExpression argument3)

Parameters

expression Expression<Func<T1, T2, T3, TResult>>

The expression to be converted to CodeDOM

argument1 CodeExpression

The first argument

argument2 CodeExpression

The second argument

argument3 CodeExpression

The third argument

Returns

CodeExpression

The CodeDOM expression that contains the equivalent code

Type Parameters

T1

The type of the first argument

T2

The type of the second argument

T3

The type of the third argument

TResult

The type of the result

ApplyExpression<T1, T2, T3, T4, TResult>(Expression<Func<T1, T2, T3, T4, TResult>>, CodeExpression, CodeExpression, CodeExpression, CodeExpression)

Applies the given expression to the given code expressions as input

public static CodeExpression ApplyExpression<T1, T2, T3, T4, TResult>(Expression<Func<T1, T2, T3, T4, TResult>> expression, CodeExpression argument1, CodeExpression argument2, CodeExpression argument3, CodeExpression argument4)

Parameters

expression Expression<Func<T1, T2, T3, T4, TResult>>

The expression to be converted to CodeDOM

argument1 CodeExpression

The first argument

argument2 CodeExpression

The second argument

argument3 CodeExpression

The third argument

argument4 CodeExpression

The fourth argument

Returns

CodeExpression

The CodeDOM expression that contains the equivalent code

Type Parameters

T1

The type of the first argument

T2

The type of the second argument

T3

The type of the third argument

T4

The type of the fourth argument

TResult

The type of the result

CreateBackingField(CodeMemberProperty)

Creates a backing field for the given property

public static CodeFieldReferenceExpression CreateBackingField(this CodeMemberProperty property)

Parameters

property CodeMemberProperty

The code property

Returns

CodeFieldReferenceExpression

A reference to the generated backing field

CreateBackingField(CodeMemberProperty, CodeTypeReference, CodeExpression)

Creates a backing field for the given property

public static CodeFieldReferenceExpression CreateBackingField(this CodeMemberProperty property, CodeTypeReference type, CodeExpression initialValue)

Parameters

property CodeMemberProperty

The code property

type CodeTypeReference

The type of the property

initialValue CodeExpression

The initial value expression for the field

Returns

CodeFieldReferenceExpression

A reference to the generated backing field

CreateOnChangedEventPattern(CodeMemberProperty)

Generates an OnChanged-pattern for the given property

public static CodeStatement CreateOnChangedEventPattern(this CodeMemberProperty property)

Parameters

property CodeMemberProperty

The code property

Returns

CodeStatement

The statement to call the OnChanged method for the given property

CreateOnChangedEventPattern(CodeMemberProperty, CodeTypeReference, CodeExpression)

Generates an OnChanged-pattern for the given property

public static CodeStatement CreateOnChangedEventPattern(this CodeMemberProperty property, CodeTypeReference eventType, CodeExpression eventData)

Parameters

property CodeMemberProperty

The code property

eventType CodeTypeReference

The event type

eventData CodeExpression

The event data

Returns

CodeStatement

The statement to call the OnChanged method for the given property

CreateOnChangingEventPattern(CodeMemberProperty)

Generates an OnChanging-pattern for the given property

public static CodeStatement CreateOnChangingEventPattern(this CodeMemberProperty property)

Parameters

property CodeMemberProperty

The code property

Returns

CodeStatement

CreateOnChangingEventPattern(CodeMemberProperty, CodeTypeReference, CodeExpression)

Generates an OnChanging-pattern for the given property

public static CodeStatement CreateOnChangingEventPattern(this CodeMemberProperty property, CodeTypeReference eventType, CodeExpression eventData)

Parameters

property CodeMemberProperty

The code property

eventType CodeTypeReference

The type of the changing event

eventData CodeExpression

The event data for the changing event

Returns

CodeStatement

A method that can be called on changing of a property

CreatePrimitiveExpression(string, CodeTypeReference, bool)

Creates a primitive expression from the serialized value

public static CodeExpression CreatePrimitiveExpression(string value, CodeTypeReference type, bool isEnum)

Parameters

value string

The serialized value

type CodeTypeReference

The type for the primitive expression

isEnum bool

True, if the value is an enum, otherwise False

Returns

CodeExpression

A code expression that represents the value as a code expression or null if there is no such expression

CreateRaiseMethod(CodeMemberEvent, CodeTypeReference)

Creates a method that raises the given event

public static CodeMemberMethod CreateRaiseMethod(this CodeMemberEvent memberEvent, CodeTypeReference eventDataType)

Parameters

memberEvent CodeMemberEvent

The event that is to be raised

eventDataType CodeTypeReference

The event arguments type

Returns

CodeMemberMethod

A code method that raises the event

CreateTypeDeclarationWithReference(string, bool)

Creates a type declaration with a reference attached to it

public static CodeTypeDeclaration CreateTypeDeclarationWithReference(string name, bool autoAssignNamespace)

Parameters

name string

The initial name of the type declaration

autoAssignNamespace bool

If true, the namespace is set in the user data such that the code generation helper can automatically deduct when the namespace can be omitted.

Returns

CodeTypeDeclaration

The generated type declaration

DependentMembers(CodeTypeMember, bool)

Gets the dependent members of a code member

public static ICollection<CodeTypeMember> DependentMembers(this CodeTypeMember item, bool createIfNecessary)

Parameters

item CodeTypeMember

The code member

createIfNecessary bool

True, if the collection of dependent members should be created if not yet existing

Returns

ICollection<CodeTypeMember>

The collection of dependent members

DependentTypes(CodeTypeMember, bool)

Gets the dependent types of a code member

public static ICollection<CodeTypeDeclaration> DependentTypes(this CodeTypeMember item, bool createIfNecessary)

Parameters

item CodeTypeMember

The code member

createIfNecessary bool

True, if the collection of dependent types should be created if not yet existing

Returns

ICollection<CodeTypeDeclaration>

The collection of dependent types or null

GetBackingField(CodeMemberProperty)

Gets the backing field for the given property

public static CodeFieldReferenceExpression GetBackingField(this CodeMemberProperty property)

Parameters

property CodeMemberProperty

The property

Returns

CodeFieldReferenceExpression

A code field reference to the backing field

GetOrCreateDefaultConstructor(CodeTypeDeclaration, Func<CodeConstructor>)

Gets or creates the default constructor for the given code type declaration

public static CodeConstructor GetOrCreateDefaultConstructor(this CodeTypeDeclaration generatedType, Func<CodeConstructor> constructorCreator = null)

Parameters

generatedType CodeTypeDeclaration

The generated type declaration

constructorCreator Func<CodeConstructor>

A function creating the default constructor if necessary

Returns

CodeConstructor

The types default constructor

GetOrCreateUserItem<TValue>(CodeObject, object, Func<TValue>)

Gets or creates the user item with the specified key

public static TValue GetOrCreateUserItem<TValue>(this CodeObject item, object key, Func<TValue> valueCreator = null) where TValue : class

Parameters

item CodeObject

The code object

key object

The key for the user item

valueCreator Func<TValue>

A method that creates the default value if the user item does not yet exist or null, if no user item should be created

Returns

TValue

The user item with the specified key

Type Parameters

TValue

The type of the user item

GetReferenceForType(CodeTypeDeclaration)

Gets the type reference associated with the given code type declaration

public static CodeTypeReference GetReferenceForType(this CodeTypeDeclaration typeDeclaration)

Parameters

typeDeclaration CodeTypeDeclaration

The code type declaration

Returns

CodeTypeReference

The type reference associated with the given type

GetTypeForReference(CodeTypeReference)

Gets the type declaration associated with the given code type reference

public static CodeTypeDeclaration GetTypeForReference(this CodeTypeReference typeReference)

Parameters

typeReference CodeTypeReference

The code type reference

Returns

CodeTypeDeclaration

The code type declaration associated with the given code type reference

GetUserCollection<TValue>(CodeObject, object, bool)

Gets the user collection associated with the given code object

public static List<TValue> GetUserCollection<TValue>(this CodeObject item, object key, bool createIfNecessary)

Parameters

item CodeObject

The code object

key object

The user key for retrieving the collection

createIfNecessary bool

True, if the collection should be created if not yet existing

Returns

List<TValue>

The user collection or null

Type Parameters

TValue

The type of the collection elements

ImplementGetter(CodeMemberProperty, CodeFieldReferenceExpression)

Implements the getter of the given property

public static void ImplementGetter(this CodeMemberProperty property, CodeFieldReferenceExpression fieldRef)

Parameters

property CodeMemberProperty

The property

fieldRef CodeFieldReferenceExpression

A reference to the underlying field

ImplementSetter(CodeMemberProperty, CodeFieldReferenceExpression, params CodeStatement[])

Implements the setter of the given property

public static void ImplementSetter(this CodeMemberProperty property, CodeFieldReferenceExpression fieldRef, params CodeStatement[] whenChanged)

Parameters

property CodeMemberProperty

The code property

fieldRef CodeFieldReferenceExpression

A reference to the underlying field

whenChanged CodeStatement[]

A collection of statements that should be performed when the value of the property changed

ImplementSetter(CodeMemberProperty, CodeFieldReferenceExpression, IEnumerable<CodeStatement>)

Implements the setter of the given property

public static void ImplementSetter(this CodeMemberProperty property, CodeFieldReferenceExpression fieldRef, IEnumerable<CodeStatement> whenChanged)

Parameters

property CodeMemberProperty

The code property

fieldRef CodeFieldReferenceExpression

A reference to the underlying field

whenChanged IEnumerable<CodeStatement>

A collection of statements that should be performed when the value of the property changed

ImpliedConstructorStatements(CodeTypeMember, bool)

Gets the implied constructor statements of the given code member

public static ICollection<CodeStatement> ImpliedConstructorStatements(this CodeTypeMember item, bool createIfNecessary)

Parameters

item CodeTypeMember

The code member

createIfNecessary bool

True, if the collection of implied statements should be created if not yet existing

Returns

ICollection<CodeStatement>

The collection of implied constructor statements

ImpliedConstructorStatementsInternal(CodeTypeMember, bool)

Gets the implied default constructor statements of the given code member

public static ICollection<CodeStatement> ImpliedConstructorStatementsInternal(CodeTypeMember item, bool createIfNecessary)

Parameters

item CodeTypeMember

The code member

createIfNecessary bool

True, if the collection of implied constructor statements should be created if not yet existing

Returns

ICollection<CodeStatement>

The collection of implied constructor statements

MarkCollectionProperty(CodeMemberProperty)

Marks the given code property as a collection property

public static void MarkCollectionProperty(this CodeMemberProperty property)

Parameters

property CodeMemberProperty

The property

Merge(CodeTypeMember, CodeTypeMember)

Merges the given members

public static CodeTypeMember Merge(this CodeTypeMember member, CodeTypeMember other)

Parameters

member CodeTypeMember

The first code member

other CodeTypeMember

The second code member

Returns

CodeTypeMember

A merged member

Namespace(CodeTypeReference)

Gets the namespace associated with the given type reference

public static string Namespace(this CodeTypeReference typeReference)

Parameters

typeReference CodeTypeReference

The type reference

Returns

string

The namespace associated with the type reference if any

SetMerge(CodeTypeMember, Func<CodeTypeMember, CodeTypeMember>)

Defines the action that should be executed when the given member needs to be nerged with another member with the same name

public static void SetMerge(this CodeTypeMember member, Func<CodeTypeMember, CodeTypeMember> mergeAction)

Parameters

member CodeTypeMember

The code member

mergeAction Func<CodeTypeMember, CodeTypeMember>

The action that should be performed in that case

SetNamespace(CodeTypeReference, string)

Associates the given type reference with the given namespace

public static void SetNamespace(this CodeTypeReference typeReference, string necessaryNamespace)

Parameters

typeReference CodeTypeReference

The type reference

necessaryNamespace string

The namespace to be associated with the given type reference

SetUserItem(CodeObject, object, object)

Overrides the user item with the given key

public static void SetUserItem(this CodeObject item, object key, object value)

Parameters

item CodeObject

The code object

key object

The user item key

value object

The value to set

Shadows(CodeTypeMember, bool)

Gets a collection of members that are being shadowed by the given code member

public static ICollection<CodeTypeMember> Shadows(this CodeTypeMember item, bool createIfNecessary)

Parameters

item CodeTypeMember

The code member

createIfNecessary bool

True, if the collection should be created if not yet existing

Returns

ICollection<CodeTypeMember>

The collection of shadowed members or null

SplitCompileUnit(CodeCompileUnit)

Splits the given code compile unit in multiple compile units to separate each type in its own compile unit

public static IDictionary<string, CodeCompileUnit> SplitCompileUnit(CodeCompileUnit unit)

Parameters

unit CodeCompileUnit

The code compile unit

Returns

IDictionary<string, CodeCompileUnit>

A dictionary of full type names and code compile units that only contain this single type

ThrowException<TException>(CodeMemberMethod, params object[])

Adds a statement to throw an exception of the given type

public static void ThrowException<TException>(this CodeMemberMethod method, params object[] arguments)

Parameters

method CodeMemberMethod

The method that should throw the exception

arguments object[]

The arguments that should be passed to the constructor of the exception

Type Parameters

TException

The type of the exception

ToTypeReference(Type)

Creates a code type reference for the given type

public static CodeTypeReference ToTypeReference(this Type type)

Parameters

type Type

The given system type for which to generate a type reference

Returns

CodeTypeReference

A code reference with namespace set accordingly

ToTypeReference(Type, params CodeTypeReference[])

Creates a code type reference for the given type

public static CodeTypeReference ToTypeReference(this Type type, params CodeTypeReference[] parameters)

Parameters

type Type

The given system type for which to generate a type reference

parameters CodeTypeReference[]

The type arguments

Returns

CodeTypeReference

A code reference with namespace set accordingly

ValidateArgument(CodeMemberMethod, string)

Generates code to validate that the given argument is not null

public static void ValidateArgument(this CodeMemberMethod method, string parameterName)

Parameters

method CodeMemberMethod

The method that should be checked

parameterName string

The parameter name

ValidateArguments(CodeMemberMethod)

Validate all arguments to be not null

public static void ValidateArguments(this CodeMemberMethod method)

Parameters

method CodeMemberMethod

The method whose arguments should be validated

WriteDocumentation(CodeMemberMethod, string, string, IDictionary<string, string>, string)

Writes documentation for the given code member

public static void WriteDocumentation(this CodeMemberMethod member, string summary, string returns, IDictionary<string, string> parameters = null, string remarks = null)

Parameters

member CodeMemberMethod

The code member

summary string

The summary documentation

returns string

The documentation for the return value

parameters IDictionary<string, string>

The documentation for the method parameters

remarks string

The documentation remarks

WriteDocumentation(CodeTypeMember, string, string)

Writes documentation for the given code member

public static void WriteDocumentation(this CodeTypeMember member, string summary, string remarks = null)

Parameters

member CodeTypeMember

The code member

summary string

The summary documentation

remarks string

The documentation remarks