本文整理汇总了C#中ICodeGenerator.GenerateCodeFromStatement方法的典型用法代码示例。如果您正苦于以下问题:C# ICodeGenerator.GenerateCodeFromStatement方法的具体用法?C# ICodeGenerator.GenerateCodeFromStatement怎么用?C# ICodeGenerator.GenerateCodeFromStatement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICodeGenerator
的用法示例。
在下文中一共展示了ICodeGenerator.GenerateCodeFromStatement方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PopulateNamespace
private static void PopulateNamespace(ICodeGenerator itfCG, TextWriter w)
{
// Add a code comment.
CodeCommentStatement c = new CodeCommentStatement("This is the wicked cool Hello class");
itfCG.GenerateCodeFromStatement(c, w, null);
// Build root namespace.
CodeNamespace cnamespace = new CodeNamespace("SimpleCodeDOMHelloClass");
// Reference other namespaces.
cnamespace.Imports.Add(new CodeNamespaceImport ("System") );
cnamespace.Imports.Add(new CodeNamespaceImport ("System.Windows.Forms") );
// Insert the HelloClass.
CodeTypeDeclaration co = new CodeTypeDeclaration ("HelloClass");
co.IsClass = true;
cnamespace.Types.Add(co);
co.BaseTypes.Add (typeof (System.Object) );
co.TypeAttributes = TypeAttributes.Public;
// Make a custom construcor.
CodeConstructor ctor = new CodeConstructor();
ctor.Attributes = MemberAttributes.Public;
ctor.Parameters.Add(new CodeParameterDeclarationExpression
(new CodeTypeReference(typeof(string)), "msg"));
ctor.Statements.Add((new CodeAssignStatement(new CodeArgumentReferenceExpression("mMsg"),
new CodeArgumentReferenceExpression("msg"))));
co.Members.Add(ctor);
// Add the default construcor.
ctor = new CodeConstructor();
ctor.Attributes = MemberAttributes.Public;
co.Members.Add(ctor);
// Insert a String field (mMsg).
CodeMemberField cf = new CodeMemberField("System.String", "mMsg");
cf.Comments.Add(new CodeCommentStatement("The state data..."));
cf.Attributes = MemberAttributes.Private;
co.Members.Add(cf);
// Add the Message property.
CodeMemberProperty cp = new CodeMemberProperty();
cp.Name = "Message";
cp.Attributes = MemberAttributes.Public | MemberAttributes.Final ;
cp.Type = new CodeTypeReference("System.String");
cp.Comments.Add(new CodeCommentStatement("The Message property"));
// Getter.
cp.GetStatements.Add(new CodeMethodReturnStatement
(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "mMsg")));
// Setter.
cp.SetStatements.Add(new CodeAssignStatement(new CodeArgumentReferenceExpression("mMsg"),
new CodeArgumentReferenceExpression("value")));
co.Members.Add (cp);
// Add the Display() message.
CodeMemberMethod cm = new CodeMemberMethod();
cm.Name = "Display";
cm.Attributes = MemberAttributes.Public | MemberAttributes.Final ;
cm.Comments.Add(new CodeCommentStatement("Show 'em what we got!"));
cm.Statements.Add (new CodeMethodInvokeExpression
(new CodeTypeReferenceExpression("MessageBox"), "Show",
new CodeExpression []
{new CodeArgumentReferenceExpression ("mMsg")}));
co.Members.Add(cm);
// Generate the code!
itfCG.GenerateCodeFromNamespace (cnamespace, w, null);
}
示例2: SpitList
} //Main()
public static void SpitList (TextWriter w, string typeName, ICodeGenerator baseCompiler, string ns)
{
CodeCommentStatement c = new CodeCommentStatement (string.Format ("List of {0}", typeName));
baseCompiler.GenerateCodeFromStatement (c, w, null);
CodeNamespace cnamespace = new CodeNamespace("Microsoft.Samples");
cnamespace.Imports.Add (new CodeNamespaceImport ("System") );
cnamespace.Imports.Add (new CodeNamespaceImport ("System.Collections") );
if (ns != null && ns != "") cnamespace.Imports.Add (new CodeNamespaceImport (ns) );
CodeTypeDeclaration co = new CodeTypeDeclaration (typeName +"List");
co.IsClass = true;
cnamespace.Types.Add (co);
co.BaseTypes.Add (typeof (CollectionBase) );
co.TypeAttributes = TypeAttributes.Public;
//Generates: public <TYPE> this[int index] {
// get {
// return ((<TYPE>)List[index]);
// }
// set {
// List[index] = value;
// }
// }
CodeMemberProperty cp = new CodeMemberProperty ();
cp.Name = "Item";
cp.Attributes = MemberAttributes.Public | MemberAttributes.Final ;
cp.Type = new CodeTypeReference(typeName);
cp.Parameters.Add (new CodeParameterDeclarationExpression (new CodeTypeReference(typeof(int)), "index"));
cp.GetStatements.Add (new CodeMethodReturnStatement (new CodeCastExpression (typeName, new CodeIndexerExpression (new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), "List"), new CodeArgumentReferenceExpression ("index")))));
cp.SetStatements.Add (new CodeAssignStatement (new CodeIndexerExpression (new CodeFieldReferenceExpression (new CodeThisReferenceExpression(),"List"), new CodeArgumentReferenceExpression ("index")), new CodeArgumentReferenceExpression ("value")));
co.Members.Add (cp);
//Gen: public int Add(<TYPE> value) {
// return List.Add(value);
// }
CodeMemberMethod cm = new CodeMemberMethod ();
cm.Name = "Add";
cm.ReturnType = new CodeTypeReference(typeof(int));
cm.Parameters.Add (new CodeParameterDeclarationExpression (typeName, "value"));
cm.Attributes = MemberAttributes.Public | MemberAttributes.Final ;
cm.Statements.Add (new CodeMethodReturnStatement (new CodeMethodInvokeExpression (new CodeFieldReferenceExpression (new CodeThisReferenceExpression(),"List"), "Add",
new CodeArgumentReferenceExpression ("value"))));
co.Members.Add (cm);
//Gen: public void Insert(int index, <TYPE> value)
// {
// List.Insert(index, info);
// }
cm = new CodeMemberMethod ();
cm.Name = "Insert";
cm.ReturnType = new CodeTypeReference(typeof(void));
cm.Parameters.Add (new CodeParameterDeclarationExpression (new CodeTypeReference(typeof(int)), "index"));
cm.Parameters.Add (new CodeParameterDeclarationExpression (typeName, "value"));
cm.Attributes = MemberAttributes.Public | MemberAttributes.Final ;
cm.Statements.Add (new CodeMethodInvokeExpression (new CodeFieldReferenceExpression (new CodeThisReferenceExpression(),"List"), "Insert",
new CodeArgumentReferenceExpression ("index"), new CodeArgumentReferenceExpression ("value")));
co.Members.Add (cm);
//Gen: public int IndexOf(<TYPE> value)
// {
// return List.IndexOf(value);
// }
cm = new CodeMemberMethod ();
cm.Name = "IndexOf";
cm.ReturnType = new CodeTypeReference(typeof(int));
cm.Parameters.Add (new CodeParameterDeclarationExpression (typeName, "value"));
cm.Attributes = MemberAttributes.Public | MemberAttributes.Final ;
cm.Statements.Add (new CodeMethodReturnStatement (new CodeMethodInvokeExpression (new CodeFieldReferenceExpression (new CodeThisReferenceExpression(),"List"), "IndexOf", new CodeExpression []
{new CodeArgumentReferenceExpression ("value")})));
co.Members.Add (cm);
//Gen: public bool Contains(<TYPE> value)
// {
// return List.Contains(value);
// }
cm = new CodeMemberMethod ();
cm.Name = "Contains";
cm.ReturnType = new CodeTypeReference(typeof(bool));
cm.Parameters.Add (new CodeParameterDeclarationExpression (typeName, "value"));
cm.Attributes = MemberAttributes.Public | MemberAttributes.Final ;
cm.Statements.Add (new CodeMethodReturnStatement (new CodeMethodInvokeExpression (new CodeFieldReferenceExpression (new CodeThisReferenceExpression(),"List"), "Contains", new CodeExpression []
{new CodeArgumentReferenceExpression ("value")})));
co.Members.Add (cm);
//Gen: public void Remove(<TYPE> value)
// {
// List.Remove(value);
// }
cm = new CodeMemberMethod ();
cm.Name = "Remove";
cm.Parameters.Add (new CodeParameterDeclarationExpression (typeName, "value"));
cm.Attributes = MemberAttributes.Public | MemberAttributes.Final ;
cm.Statements.Add (new CodeMethodInvokeExpression (new CodeFieldReferenceExpression (new CodeThisReferenceExpression(),"List"), "Remove", new CodeExpression []
{new CodeArgumentReferenceExpression ("value")}));
//.........这里部分代码省略.........