Class CodeDomHelper
A helper class to generate code using CodeDOM
Inherited Members
Namespace: NMF.CodeGen
Assembly: NMF.CodeGen.dll
Syntax
public static class CodeDomHelper
Methods
| Edit this page View SourceAddAttribute(CodeTypeMember, Type, params object[])
Adds an attribute of the given attribute type to the given code member
Declaration
public static void AddAttribute(this CodeTypeMember member, Type attributeType, params object[] arguments)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeMember | member | The code member |
Type | attributeType | The type of the attribute |
object[] | arguments | 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
Declaration
public static CodeExpression ApplyExpression<T1, TResult>(Expression<Func<T1, TResult>> expression, CodeExpression argument)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T1, TResult>> | expression | The expression to be converted to CodeDOM |
CodeExpression | argument | The first argument |
Returns
Type | Description |
---|---|
CodeExpression | The CodeDOM expression that contains the equivalent code |
Type Parameters
Name | Description |
---|---|
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
Declaration
public static CodeExpression ApplyExpression<T1, T2, TResult>(Expression<Func<T1, T2, TResult>> expression, CodeExpression argument1, CodeExpression argument2)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T1, T2, TResult>> | expression | The expression to be converted to CodeDOM |
CodeExpression | argument1 | The first argument |
CodeExpression | argument2 | The second argument |
Returns
Type | Description |
---|---|
CodeExpression | The CodeDOM expression that contains the equivalent code |
Type Parameters
Name | Description |
---|---|
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
Declaration
public static CodeExpression ApplyExpression<T1, T2, T3, TResult>(Expression<Func<T1, T2, T3, TResult>> expression, CodeExpression argument1, CodeExpression argument2, CodeExpression argument3)
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T1, T2, T3, TResult>> | expression | The expression to be converted to CodeDOM |
CodeExpression | argument1 | The first argument |
CodeExpression | argument2 | The second argument |
CodeExpression | argument3 | The third argument |
Returns
Type | Description |
---|---|
CodeExpression | The CodeDOM expression that contains the equivalent code |
Type Parameters
Name | Description |
---|---|
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
Declaration
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
Type | Name | Description |
---|---|---|
Expression<Func<T1, T2, T3, T4, TResult>> | expression | The expression to be converted to CodeDOM |
CodeExpression | argument1 | The first argument |
CodeExpression | argument2 | The second argument |
CodeExpression | argument3 | The third argument |
CodeExpression | argument4 | The fourth argument |
Returns
Type | Description |
---|---|
CodeExpression | The CodeDOM expression that contains the equivalent code |
Type Parameters
Name | Description |
---|---|
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
Declaration
public static CodeFieldReferenceExpression CreateBackingField(this CodeMemberProperty property)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The code property |
Returns
Type | Description |
---|---|
CodeFieldReferenceExpression | A reference to the generated backing field |
CreateBackingField(CodeMemberProperty, CodeTypeReference, CodeExpression)
Creates a backing field for the given property
Declaration
public static CodeFieldReferenceExpression CreateBackingField(this CodeMemberProperty property, CodeTypeReference type, CodeExpression initialValue)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The code property |
CodeTypeReference | type | The type of the property |
CodeExpression | initialValue | The initial value expression for the field |
Returns
Type | Description |
---|---|
CodeFieldReferenceExpression | A reference to the generated backing field |
CreateOnChangedEventPattern(CodeMemberProperty)
Generates an OnChanged-pattern for the given property
Declaration
public static CodeStatement CreateOnChangedEventPattern(this CodeMemberProperty property)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The code property |
Returns
Type | Description |
---|---|
CodeStatement | The statement to call the OnChanged method for the given property |
CreateOnChangedEventPattern(CodeMemberProperty, CodeTypeReference, CodeExpression)
Generates an OnChanged-pattern for the given property
Declaration
public static CodeStatement CreateOnChangedEventPattern(this CodeMemberProperty property, CodeTypeReference eventType, CodeExpression eventData)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The code property |
CodeTypeReference | eventType | The event type |
CodeExpression | eventData | The event data |
Returns
Type | Description |
---|---|
CodeStatement | The statement to call the OnChanged method for the given property |
CreateOnChangingEventPattern(CodeMemberProperty)
Generates an OnChanging-pattern for the given property
Declaration
public static CodeStatement CreateOnChangingEventPattern(this CodeMemberProperty property)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The code property |
Returns
Type | Description |
---|---|
CodeStatement |
CreateOnChangingEventPattern(CodeMemberProperty, CodeTypeReference, CodeExpression)
Generates an OnChanging-pattern for the given property
Declaration
public static CodeStatement CreateOnChangingEventPattern(this CodeMemberProperty property, CodeTypeReference eventType, CodeExpression eventData)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The code property |
CodeTypeReference | eventType | The type of the changing event |
CodeExpression | eventData | The event data for the changing event |
Returns
Type | Description |
---|---|
CodeStatement | A method that can be called on changing of a property |
CreatePrimitiveExpression(string, CodeTypeReference, bool)
Creates a primitive expression from the serialized value
Declaration
public static CodeExpression CreatePrimitiveExpression(string value, CodeTypeReference type, bool isEnum)
Parameters
Type | Name | Description |
---|---|---|
string | value | The serialized value |
CodeTypeReference | type | The type for the primitive expression |
bool | isEnum | True, if the value is an enum, otherwise False |
Returns
Type | Description |
---|---|
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
Declaration
public static CodeMemberMethod CreateRaiseMethod(this CodeMemberEvent memberEvent, CodeTypeReference eventDataType)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberEvent | memberEvent | The event that is to be raised |
CodeTypeReference | eventDataType | The event arguments type |
Returns
Type | Description |
---|---|
CodeMemberMethod | A code method that raises the event |
CreateTypeDeclarationWithReference(string, bool)
Creates a type declaration with a reference attached to it
Declaration
public static CodeTypeDeclaration CreateTypeDeclarationWithReference(string name, bool autoAssignNamespace)
Parameters
Type | Name | Description |
---|---|---|
string | name | The initial name of the type declaration |
bool | autoAssignNamespace | 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
Type | Description |
---|---|
CodeTypeDeclaration | The generated type declaration |
DependentMembers(CodeTypeMember, bool)
Gets the dependent members of a code member
Declaration
public static ICollection<CodeTypeMember> DependentMembers(this CodeTypeMember item, bool createIfNecessary)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeMember | item | The code member |
bool | createIfNecessary | True, if the collection of dependent members should be created if not yet existing |
Returns
Type | Description |
---|---|
ICollection<CodeTypeMember> | The collection of dependent members |
DependentTypes(CodeTypeMember, bool)
Gets the dependent types of a code member
Declaration
public static ICollection<CodeTypeDeclaration> DependentTypes(this CodeTypeMember item, bool createIfNecessary)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeMember | item | The code member |
bool | createIfNecessary | True, if the collection of dependent types should be created if not yet existing |
Returns
Type | Description |
---|---|
ICollection<CodeTypeDeclaration> | The collection of dependent types or null |
GetBackingField(CodeMemberProperty)
Gets the backing field for the given property
Declaration
public static CodeFieldReferenceExpression GetBackingField(this CodeMemberProperty property)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The property |
Returns
Type | Description |
---|---|
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
Declaration
public static CodeConstructor GetOrCreateDefaultConstructor(this CodeTypeDeclaration generatedType, Func<CodeConstructor> constructorCreator = null)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeDeclaration | generatedType | The generated type declaration |
Func<CodeConstructor> | constructorCreator | A function creating the default constructor if necessary |
Returns
Type | Description |
---|---|
CodeConstructor | The types default constructor |
GetOrCreateUserItem<TValue>(CodeObject, object, Func<TValue>)
Gets or creates the user item with the specified key
Declaration
public static TValue GetOrCreateUserItem<TValue>(this CodeObject item, object key, Func<TValue> valueCreator = null) where TValue : class
Parameters
Type | Name | Description |
---|---|---|
CodeObject | item | The code object |
object | key | The key for the user item |
Func<TValue> | valueCreator | 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
Type | Description |
---|---|
TValue | The user item with the specified key |
Type Parameters
Name | Description |
---|---|
TValue | The type of the user item |
GetReferenceForType(CodeTypeDeclaration)
Gets the type reference associated with the given code type declaration
Declaration
public static CodeTypeReference GetReferenceForType(this CodeTypeDeclaration typeDeclaration)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeDeclaration | typeDeclaration | The code type declaration |
Returns
Type | Description |
---|---|
CodeTypeReference | The type reference associated with the given type |
GetTypeForReference(CodeTypeReference)
Gets the type declaration associated with the given code type reference
Declaration
public static CodeTypeDeclaration GetTypeForReference(this CodeTypeReference typeReference)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeReference | typeReference | The code type reference |
Returns
Type | Description |
---|---|
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
Declaration
public static List<TValue> GetUserCollection<TValue>(this CodeObject item, object key, bool createIfNecessary)
Parameters
Type | Name | Description |
---|---|---|
CodeObject | item | The code object |
object | key | The user key for retrieving the collection |
bool | createIfNecessary | True, if the collection should be created if not yet existing |
Returns
Type | Description |
---|---|
List<TValue> | The user collection or null |
Type Parameters
Name | Description |
---|---|
TValue | The type of the collection elements |
ImplementGetter(CodeMemberProperty, CodeFieldReferenceExpression)
Implements the getter of the given property
Declaration
public static void ImplementGetter(this CodeMemberProperty property, CodeFieldReferenceExpression fieldRef)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The property |
CodeFieldReferenceExpression | fieldRef | A reference to the underlying field |
ImplementSetter(CodeMemberProperty, CodeFieldReferenceExpression, params CodeStatement[])
Implements the setter of the given property
Declaration
public static void ImplementSetter(this CodeMemberProperty property, CodeFieldReferenceExpression fieldRef, params CodeStatement[] whenChanged)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The code property |
CodeFieldReferenceExpression | fieldRef | A reference to the underlying field |
CodeStatement[] | whenChanged | 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
Declaration
public static void ImplementSetter(this CodeMemberProperty property, CodeFieldReferenceExpression fieldRef, IEnumerable<CodeStatement> whenChanged)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The code property |
CodeFieldReferenceExpression | fieldRef | A reference to the underlying field |
IEnumerable<CodeStatement> | whenChanged | 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
Declaration
public static ICollection<CodeStatement> ImpliedConstructorStatements(this CodeTypeMember item, bool createIfNecessary)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeMember | item | The code member |
bool | createIfNecessary | True, if the collection of implied statements should be created if not yet existing |
Returns
Type | Description |
---|---|
ICollection<CodeStatement> | The collection of implied constructor statements |
ImpliedConstructorStatementsInternal(CodeTypeMember, bool)
Gets the implied default constructor statements of the given code member
Declaration
public static ICollection<CodeStatement> ImpliedConstructorStatementsInternal(CodeTypeMember item, bool createIfNecessary)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeMember | item | The code member |
bool | createIfNecessary | True, if the collection of implied constructor statements should be created if not yet existing |
Returns
Type | Description |
---|---|
ICollection<CodeStatement> | The collection of implied constructor statements |
MarkCollectionProperty(CodeMemberProperty)
Marks the given code property as a collection property
Declaration
public static void MarkCollectionProperty(this CodeMemberProperty property)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberProperty | property | The property |
Merge(CodeTypeMember, CodeTypeMember)
Merges the given members
Declaration
public static CodeTypeMember Merge(this CodeTypeMember member, CodeTypeMember other)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeMember | member | The first code member |
CodeTypeMember | other | The second code member |
Returns
Type | Description |
---|---|
CodeTypeMember | A merged member |
Namespace(CodeTypeReference)
Gets the namespace associated with the given type reference
Declaration
public static string Namespace(this CodeTypeReference typeReference)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeReference | typeReference | The type reference |
Returns
Type | Description |
---|---|
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
Declaration
public static void SetMerge(this CodeTypeMember member, Func<CodeTypeMember, CodeTypeMember> mergeAction)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeMember | member | The code member |
Func<CodeTypeMember, CodeTypeMember> | mergeAction | The action that should be performed in that case |
SetNamespace(CodeTypeReference, string)
Associates the given type reference with the given namespace
Declaration
public static void SetNamespace(this CodeTypeReference typeReference, string necessaryNamespace)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeReference | typeReference | The type reference |
string | necessaryNamespace | The namespace to be associated with the given type reference |
SetUserItem(CodeObject, object, object)
Overrides the user item with the given key
Declaration
public static void SetUserItem(this CodeObject item, object key, object value)
Parameters
Type | Name | Description |
---|---|---|
CodeObject | item | The code object |
object | key | The user item key |
object | value | The value to set |
Shadows(CodeTypeMember, bool)
Gets a collection of members that are being shadowed by the given code member
Declaration
public static ICollection<CodeTypeMember> Shadows(this CodeTypeMember item, bool createIfNecessary)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeMember | item | The code member |
bool | createIfNecessary | True, if the collection should be created if not yet existing |
Returns
Type | Description |
---|---|
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
Declaration
public static IDictionary<string, CodeCompileUnit> SplitCompileUnit(CodeCompileUnit unit)
Parameters
Type | Name | Description |
---|---|---|
CodeCompileUnit | unit | The code compile unit |
Returns
Type | Description |
---|---|
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
Declaration
public static void ThrowException<TException>(this CodeMemberMethod method, params object[] arguments)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberMethod | method | The method that should throw the exception |
object[] | arguments | The arguments that should be passed to the constructor of the exception |
Type Parameters
Name | Description |
---|---|
TException | The type of the exception |
ToTypeReference(Type)
Creates a code type reference for the given type
Declaration
public static CodeTypeReference ToTypeReference(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The given system type for which to generate a type reference |
Returns
Type | Description |
---|---|
CodeTypeReference | A code reference with namespace set accordingly |
ToTypeReference(Type, params CodeTypeReference[])
Creates a code type reference for the given type
Declaration
public static CodeTypeReference ToTypeReference(this Type type, params CodeTypeReference[] parameters)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The given system type for which to generate a type reference |
CodeTypeReference[] | parameters | The type arguments |
Returns
Type | Description |
---|---|
CodeTypeReference | A code reference with namespace set accordingly |
ValidateArgument(CodeMemberMethod, string)
Generates code to validate that the given argument is not null
Declaration
public static void ValidateArgument(this CodeMemberMethod method, string parameterName)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberMethod | method | The method that should be checked |
string | parameterName | The parameter name |
ValidateArguments(CodeMemberMethod)
Validate all arguments to be not null
Declaration
public static void ValidateArguments(this CodeMemberMethod method)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberMethod | method | The method whose arguments should be validated |
WriteDocumentation(CodeMemberMethod, string, string, IDictionary<string, string>, string)
Writes documentation for the given code member
Declaration
public static void WriteDocumentation(this CodeMemberMethod member, string summary, string returns, IDictionary<string, string> parameters = null, string remarks = null)
Parameters
Type | Name | Description |
---|---|---|
CodeMemberMethod | member | The code member |
string | summary | The summary documentation |
string | returns | The documentation for the return value |
IDictionary<string, string> | parameters | The documentation for the method parameters |
string | remarks | The documentation remarks |
WriteDocumentation(CodeTypeMember, string, string)
Writes documentation for the given code member
Declaration
public static void WriteDocumentation(this CodeTypeMember member, string summary, string remarks = null)
Parameters
Type | Name | Description |
---|---|---|
CodeTypeMember | member | The code member |
string | summary | The summary documentation |
string | remarks | The documentation remarks |