Class CodeDomHelper
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
CodeTypeMemberThe code member
attributeType
TypeThe 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
CodeExpressionThe 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
CodeExpressionThe first argument
argument2
CodeExpressionThe 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
CodeExpressionThe first argument
argument2
CodeExpressionThe second argument
argument3
CodeExpressionThe 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
CodeExpressionThe first argument
argument2
CodeExpressionThe second argument
argument3
CodeExpressionThe third argument
argument4
CodeExpressionThe 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
CodeMemberPropertyThe 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
CodeMemberPropertyThe code property
type
CodeTypeReferenceThe type of the property
initialValue
CodeExpressionThe 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
CodeMemberPropertyThe 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
CodeMemberPropertyThe code property
eventType
CodeTypeReferenceThe event type
eventData
CodeExpressionThe 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
CodeMemberPropertyThe code property
Returns
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
CodeMemberPropertyThe code property
eventType
CodeTypeReferenceThe type of the changing event
eventData
CodeExpressionThe 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
stringThe serialized value
type
CodeTypeReferenceThe type for the primitive expression
isEnum
boolTrue, 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
CodeMemberEventThe event that is to be raised
eventDataType
CodeTypeReferenceThe 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
stringThe initial name of the type declaration
autoAssignNamespace
boolIf 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
CodeTypeMemberThe code member
createIfNecessary
boolTrue, 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
CodeTypeMemberThe code member
createIfNecessary
boolTrue, 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
CodeMemberPropertyThe 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
CodeTypeDeclarationThe 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
CodeObjectThe code object
key
objectThe 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
CodeTypeDeclarationThe 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
CodeTypeReferenceThe 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
CodeObjectThe code object
key
objectThe user key for retrieving the collection
createIfNecessary
boolTrue, 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
CodeMemberPropertyThe property
fieldRef
CodeFieldReferenceExpressionA 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
CodeMemberPropertyThe code property
fieldRef
CodeFieldReferenceExpressionA 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
CodeMemberPropertyThe code property
fieldRef
CodeFieldReferenceExpressionA 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
CodeTypeMemberThe code member
createIfNecessary
boolTrue, 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
CodeTypeMemberThe code member
createIfNecessary
boolTrue, 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
CodeMemberPropertyThe property
Merge(CodeTypeMember, CodeTypeMember)
Merges the given members
public static CodeTypeMember Merge(this CodeTypeMember member, CodeTypeMember other)
Parameters
member
CodeTypeMemberThe first code member
other
CodeTypeMemberThe 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
CodeTypeReferenceThe 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
CodeTypeMemberThe 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
CodeTypeReferenceThe type reference
necessaryNamespace
stringThe 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
CodeObjectThe code object
key
objectThe user item key
value
objectThe 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
CodeTypeMemberThe code member
createIfNecessary
boolTrue, 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
CodeCompileUnitThe 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
CodeMemberMethodThe 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
TypeThe 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
TypeThe 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
CodeMemberMethodThe method that should be checked
parameterName
stringThe parameter name
ValidateArguments(CodeMemberMethod)
Validate all arguments to be not null
public static void ValidateArguments(this CodeMemberMethod method)
Parameters
method
CodeMemberMethodThe 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
CodeMemberMethodThe code member
summary
stringThe summary documentation
returns
stringThe documentation for the return value
parameters
IDictionary<string, string>The documentation for the method parameters
remarks
stringThe 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
CodeTypeMemberThe code member
summary
stringThe summary documentation
remarks
stringThe documentation remarks