// --------------------------------------------------------------------------------------------------------------------
//
// N/A
//
//
// Code DOM manipulation helper methods
//
// --------------------------------------------------------------------------------------------------------------------
using System.ComponentModel;
namespace Xsd2Code.Library.Helpers
{
using System;
using System.CodeDom;
using System.Collections.Generic;
///
/// Code DOM manipulation helper methods
///
internal static class CodeDomHelper
{
///
/// Add to CodeCommentStatementCollection summary documentation
///
/// Collection of CodeCommentStatement
/// summary text
internal static void CreateSummaryComment(CodeCommentStatementCollection codeStatmentColl, string comment)
{
codeStatmentColl.Add(new CodeCommentStatement("", true));
string[] lines = comment.Split(new[] { '\n' });
foreach (string line in lines)
codeStatmentColl.Add(new CodeCommentStatement(line.Trim(), true));
codeStatmentColl.Add(new CodeCommentStatement("", true));
}
///
/// Creates the object.
///
/// Type of the object.
/// object name.
/// The c tor parameter.
/// return variable declaration
internal static CodeVariableDeclarationStatement CreateObject(Type objectType, string objectName, params string[] ctorParams)
{
var ce = new List();
foreach (var item in ctorParams)
ce.Add(new CodeTypeReferenceExpression(item));
return CreateObject(objectType, objectName, ce.ToArray());
}
///
/// Creates the object.
///
/// Type of the object.
/// Name of the object.
/// The ctor params.
/// return variable declaration
internal static CodeVariableDeclarationStatement CreateObject(Type objectType, string objectName, params CodeExpression[] ctorParams)
{
return new CodeVariableDeclarationStatement(
new CodeTypeReference(objectType),
objectName,
new CodeObjectCreateExpression(new CodeTypeReference(objectType), ctorParams));
}
///
/// Get CodeMethodInvokeExpression
///
/// Name of target object. Use this if empty
/// Name of method to invoke
/// CodeMethodInvokeExpression value
internal static CodeMethodInvokeExpression GetInvokeMethod(string targetObject, string methodName)
{
return GetInvokeMethod(targetObject, methodName, null);
}
///
/// Get CodeMethodInvokeExpression
///
/// Name of target object. Use this if empty
/// Name of method to invoke
/// method params
/// CodeMethodInvokeExpression value
internal static CodeMethodInvokeExpression GetInvokeMethod(string targetObject, string methodName, CodeExpression[] parameters)
{
var methodInvoke =
parameters != null
? new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(targetObject), methodName), parameters)
: new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(targetObject), methodName));
return methodInvoke;
}
///
/// Getr return true statment
///
/// statment of return code
internal static CodeMethodReturnStatement GetReturnTrue()
{
return new CodeMethodReturnStatement(new CodePrimitiveExpression(true));
}
///
/// Get return false startment
///
/// statment of return code
internal static CodeMethodReturnStatement GetReturnFalse()
{
return new CodeMethodReturnStatement(new CodePrimitiveExpression(false));
}
///
/// Return catch statments
///
/// CodeCatchClause statments
internal static CodeCatchClause[] GetCatchClause()
{
var catchStatmanents = new CodeStatement[2];
catchStatmanents[0] = new CodeAssignStatement(
new CodeVariableReferenceExpression("exception"),
new CodeVariableReferenceExpression("ex"));
catchStatmanents[1] = GetReturnFalse();
var catchClause = new CodeCatchClause(
"ex",
new CodeTypeReference(typeof(Exception)),
catchStatmanents);
var catchClauses = new[] { catchClause };
return catchClauses;
}
///
/// Gets the throw clause.
///
/// return catch...throw statment
internal static CodeCatchClause[] GetThrowClause()
{
var catchStatmanents = new CodeStatementCollection();
catchStatmanents.Add(new CodeThrowExceptionStatement(new CodeVariableReferenceExpression("ex")));
var catchClause = new CodeCatchClause(
"ex",
new CodeTypeReference(typeof(Exception)),
catchStatmanents.ToArray());
var catchClauses = new[] { catchClause };
return catchClauses;
}
///
/// Codes the STMT col to array.
///
/// The statment collection.
/// return CodeStmtColToArray
internal static CodeStatement[] CodeStmtColToArray(CodeStatementCollection statmentCollection)
{
var tryFinallyStatmanents = new CodeStatement[statmentCollection.Count];
statmentCollection.CopyTo(tryFinallyStatmanents, 0);
return tryFinallyStatmanents;
}
///
/// Get return CodeCommentStatement comment
///
/// Return text comment
/// return return comment statment
internal static CodeCommentStatement GetReturnComment(string text)
{
var comments = new CodeCommentStatement(string.Format("{0}", text),true);
return comments;
}
///
/// Get summary CodeCommentStatementCollection comment
///
/// Summary text comment
/// CodeCommentStatementCollection comment
internal static CodeCommentStatementCollection GetSummaryComment(string text)
{
var comments = new CodeCommentStatementCollection
{
new CodeCommentStatement("", true),
new CodeCommentStatement(text, true),
new CodeCommentStatement("", true)
};
return comments;
}
///
/// Get param comment statment
///
/// Param Name
/// param summary
/// CodeCommentStatement param
internal static CodeCommentStatement GetParamComment(string paramName, string text)
{
var comments = new CodeCommentStatement(string.Format("{1}", paramName, text),true);
return comments;
}
///
/// Transform CodeStatementCollection into CodeStatement[]
///
/// The collection.
/// array of CodeStatement
internal static CodeStatement[] ToArray(this CodeStatementCollection collection)
{
CodeStatement[] cdst = null;
if (collection != null)
{
cdst = new CodeStatement[collection.Count];
collection.CopyTo(cdst, 0);
}
return cdst;
}
///
/// Gets the dispose.
///
/// Name of the object.
/// return dispose CodeDom
internal static CodeConditionStatement GetDispose(string objectName)
{
var statments = new CodeStatementCollection();
statments.Add(GetInvokeMethod(objectName, "Dispose"));
return
new CodeConditionStatement(
new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression(objectName),
CodeBinaryOperatorType.IdentityInequality,
new CodePrimitiveExpression(null)),
statments.ToArray());
}
///
/// Creates the instance.
///
/// The instance type.
/// The ctor params.
/// return code of new objectinstance
internal static CodeObjectCreateExpression CreateInstance(Type type)
{
return new CodeObjectCreateExpression(new CodeTypeReference(type));
}
///
/// Creates the instance.
///
/// The instance type.
/// The ctor params.
/// return code of new objectinstance
internal static CodeObjectCreateExpression CreateInstance(Type type, params string[] ctorParams)
{
var ce = new List();
foreach (var item in ctorParams)
ce.Add(new CodeTypeReferenceExpression(item));
return CreateInstance(type, ce.ToArray());
}
///
/// Creates the instance.
///
/// The instance type.
/// The ctor params.
/// return code of new objectinstance
internal static CodeObjectCreateExpression CreateInstance(Type type, CodeTypeReferenceExpression[] ctorParams)
{
return new CodeObjectCreateExpression(new CodeTypeReference(type), ctorParams);
}
//DCM ADDED: Helpers to remove More CodeSnippetExpressions
#region DCM Added 2010-01-20
///
/// Get CodeMethodInvokeExpression
///
/// Name of target object. Use this if empty
/// Name of method to invoke
/// method params as variable argument array
/// CodeMethodInvokeExpression value
/// DCM ADDED for varArgs
internal static CodeMethodInvokeExpression GetInvokeMethodEx(string targetObject, string methodName, params CodeExpression[] parameters)
{
var methodInvoke =
parameters != null
? new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(targetObject), methodName), parameters)
: new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(targetObject), methodName));
return methodInvoke;
}
///
/// Gets the Enum CodeFieldReferenceExpression.
///
/// Type of the enum.
/// Name of the field.
///
internal static CodeFieldReferenceExpression GetEnum(Type enumType, String fieldName)
{
return new CodeFieldReferenceExpression(
new CodeTypeReferenceExpression(enumType), fieldName);
}
///
/// Gets the enum as CodeFieldReferenceExpression.
///
/// Type of the enum as string.
/// Name of the field.
///
internal static CodeFieldReferenceExpression GetEnum(String enumType, String fieldName)
{
return new CodeFieldReferenceExpression(
new CodeTypeReferenceExpression(enumType), fieldName);
}
///
/// Gets the Static Field CodeFieldReferenceExpression.
///
/// Type of the Class.
/// Name of the field.
///
internal static CodeFieldReferenceExpression GetStaticField(Type classType, String fieldName)
{
return new CodeFieldReferenceExpression(
new CodeTypeReferenceExpression(classType), fieldName);
}
///
/// Gets the Static Field as CodeFieldReferenceExpression.
///
/// Type of the Class as string.
/// Name of the field.
///
internal static CodeFieldReferenceExpression GetStaticField(String classType, String fieldName)
{
return new CodeFieldReferenceExpression(
new CodeTypeReferenceExpression(classType), fieldName);
}
///
/// Gets the object's named property.
///
/// The target object.
/// Name of the property.
///
internal static CodePropertyReferenceExpression GetObjectProperty(string targetObject, string propertyName)
{
return new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(targetObject),propertyName);
}
///
/// Searches the CodeTypeDeclaration and Gets the name member method.
///
/// The type.
/// The name.
///
internal static CodeMemberMethod GetMemberMethod(CodeTypeDeclaration type, string name )
{
CodeMemberMethod result = null;
foreach (CodeTypeMember member in type.Members)
{
if (member is CodeMemberMethod && member.Name.Equals(name))
result = member as CodeMemberMethod;
}
return result;
}
///
/// Creates the property changed method.
///
/// CodeMemberMethod on Property Change handler
///
///
/// protected virtual void OnPropertyChanged(string info) {
/// PropertyChangedEventHandler handler = PropertyChanged;
/// if (handler != null) {
/// handler(this, new PropertyChangedEventArgs(info));
/// }
/// }
///
///
internal static CodeMemberMethod CreatePropertyChangedMethod()
{
const string paramName = "info";
const string variableName = "handler";
var propertyChangedMethod = new CodeMemberMethod
{
Name = "OnPropertyChanged",
Attributes = MemberAttributes.Public
};
propertyChangedMethod.Parameters.Add(new CodeParameterDeclarationExpression(
new CodeTypeReference(typeof(String)), paramName));
//Declare temp variable holding the event
var vardec = new CodeVariableDeclarationStatement(
new CodeTypeReference(typeof(PropertyChangedEventHandler)), variableName);
vardec.InitExpression = new CodeEventReferenceExpression(
new CodeThisReferenceExpression(), "PropertyChanged");
propertyChangedMethod.Statements.Add(vardec);
//The part of the true, create the event and invoke it
//var createArgs = new CodeObjectCreateExpression(
// new CodeTypeReference(typeof(PropertyChangedEventArgs)),
// new CodeArgumentReferenceExpression(paramName));
var createArgs = CodeDomHelper.CreateInstance(typeof(PropertyChangedEventArgs), paramName);
var raiseEvent = new CodeDelegateInvokeExpression(
new CodeVariableReferenceExpression(variableName),
new CodeThisReferenceExpression(), createArgs);
//The Condition
CodeExpression condition = new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression(variableName),
CodeBinaryOperatorType.IdentityInequality,
new CodePrimitiveExpression(null));
//The if condition
var ifTempIsNull = new CodeConditionStatement();
ifTempIsNull.Condition = condition;
ifTempIsNull.TrueStatements.Add(raiseEvent);
propertyChangedMethod.Statements.Add(ifTempIsNull);
return propertyChangedMethod;
}
#endregion
}
}