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
memberCodeTypeMemberThe code member
attributeTypeTypeThe type of the attribute
argumentsobject[]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
expressionExpression<Func<T1, TResult>>The expression to be converted to CodeDOM
argumentCodeExpressionThe first argument
Returns
- CodeExpression
The CodeDOM expression that contains the equivalent code
Type Parameters
T1The type of the first argument
TResultThe 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
expressionExpression<Func<T1, T2, TResult>>The expression to be converted to CodeDOM
argument1CodeExpressionThe first argument
argument2CodeExpressionThe second argument
Returns
- CodeExpression
The CodeDOM expression that contains the equivalent code
Type Parameters
T1The type of the first argument
T2The type of the second argument
TResultThe 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
expressionExpression<Func<T1, T2, T3, TResult>>The expression to be converted to CodeDOM
argument1CodeExpressionThe first argument
argument2CodeExpressionThe second argument
argument3CodeExpressionThe third argument
Returns
- CodeExpression
The CodeDOM expression that contains the equivalent code
Type Parameters
T1The type of the first argument
T2The type of the second argument
T3The type of the third argument
TResultThe 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
expressionExpression<Func<T1, T2, T3, T4, TResult>>The expression to be converted to CodeDOM
argument1CodeExpressionThe first argument
argument2CodeExpressionThe second argument
argument3CodeExpressionThe third argument
argument4CodeExpressionThe fourth argument
Returns
- CodeExpression
The CodeDOM expression that contains the equivalent code
Type Parameters
T1The type of the first argument
T2The type of the second argument
T3The type of the third argument
T4The type of the fourth argument
TResultThe type of the result
CreateBackingField(CodeMemberProperty)
Creates a backing field for the given property
public static CodeFieldReferenceExpression CreateBackingField(this CodeMemberProperty property)
Parameters
propertyCodeMemberPropertyThe 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
propertyCodeMemberPropertyThe code property
typeCodeTypeReferenceThe type of the property
initialValueCodeExpressionThe 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
propertyCodeMemberPropertyThe 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
propertyCodeMemberPropertyThe code property
eventTypeCodeTypeReferenceThe event type
eventDataCodeExpressionThe 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
propertyCodeMemberPropertyThe 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
propertyCodeMemberPropertyThe code property
eventTypeCodeTypeReferenceThe type of the changing event
eventDataCodeExpressionThe 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
valuestringThe serialized value
typeCodeTypeReferenceThe type for the primitive expression
isEnumboolTrue, 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
memberEventCodeMemberEventThe event that is to be raised
eventDataTypeCodeTypeReferenceThe 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
namestringThe initial name of the type declaration
autoAssignNamespaceboolIf 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
itemCodeTypeMemberThe code member
createIfNecessaryboolTrue, 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
itemCodeTypeMemberThe code member
createIfNecessaryboolTrue, 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
propertyCodeMemberPropertyThe 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
generatedTypeCodeTypeDeclarationThe generated type declaration
constructorCreatorFunc<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
itemCodeObjectThe code object
keyobjectThe key for the user item
valueCreatorFunc<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
TValueThe 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
typeDeclarationCodeTypeDeclarationThe 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
typeReferenceCodeTypeReferenceThe 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
itemCodeObjectThe code object
keyobjectThe user key for retrieving the collection
createIfNecessaryboolTrue, if the collection should be created if not yet existing
Returns
- List<TValue>
The user collection or null
Type Parameters
TValueThe 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
propertyCodeMemberPropertyThe property
fieldRefCodeFieldReferenceExpressionA 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
propertyCodeMemberPropertyThe code property
fieldRefCodeFieldReferenceExpressionA reference to the underlying field
whenChangedCodeStatement[]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
propertyCodeMemberPropertyThe code property
fieldRefCodeFieldReferenceExpressionA reference to the underlying field
whenChangedIEnumerable<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
itemCodeTypeMemberThe code member
createIfNecessaryboolTrue, 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
itemCodeTypeMemberThe code member
createIfNecessaryboolTrue, 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
propertyCodeMemberPropertyThe property
Merge(CodeTypeMember, CodeTypeMember)
Merges the given members
public static CodeTypeMember Merge(this CodeTypeMember member, CodeTypeMember other)
Parameters
memberCodeTypeMemberThe first code member
otherCodeTypeMemberThe 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
typeReferenceCodeTypeReferenceThe 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
memberCodeTypeMemberThe code member
mergeActionFunc<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
typeReferenceCodeTypeReferenceThe type reference
necessaryNamespacestringThe 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
itemCodeObjectThe code object
keyobjectThe user item key
valueobjectThe 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
itemCodeTypeMemberThe code member
createIfNecessaryboolTrue, 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
unitCodeCompileUnitThe 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
methodCodeMemberMethodThe method that should throw the exception
argumentsobject[]The arguments that should be passed to the constructor of the exception
Type Parameters
TExceptionThe type of the exception
ToTypeReference(Type)
Creates a code type reference for the given type
public static CodeTypeReference ToTypeReference(this Type type)
Parameters
typeTypeThe 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
typeTypeThe given system type for which to generate a type reference
parametersCodeTypeReference[]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
methodCodeMemberMethodThe method that should be checked
parameterNamestringThe parameter name
ValidateArguments(CodeMemberMethod)
Validate all arguments to be not null
public static void ValidateArguments(this CodeMemberMethod method)
Parameters
methodCodeMemberMethodThe 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
memberCodeMemberMethodThe code member
summarystringThe summary documentation
returnsstringThe documentation for the return value
parametersIDictionary<string, string>The documentation for the method parameters
remarksstringThe 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
memberCodeTypeMemberThe code member
summarystringThe summary documentation
remarksstringThe documentation remarks