public static void Emit(CodeNamespace codeNamespace, Interface inter) { var codeType = new CodeTypeDeclaration(); codeNamespace.Types.Add(codeType); codeType.Name = inter.UnqualifiedName; var typeAttr = TypeAttributes.Interface; // Add the list of generic type names of the class. foreach (string s in inter.GenericTypeNames) { codeType.TypeParameters.Add(new CodeTypeParameter(s)); } // Add the list of base type names of the class. foreach (string s in inter.BaseTypeNames) { codeType.BaseTypes.Add(s); } // Set the accessibility of the class. switch (inter.Accessibility) { case Accessibility.Internal: typeAttr |= TypeAttributes.NestedAssembly; break; case Accessibility.Public: typeAttr |= TypeAttributes.Public; break; } codeType.TypeAttributes = typeAttr; // Emit interface methods and properties. foreach (var e in inter.ChildExpressions) { if (e is MethodDeclaration) { MethodEmitter.Emit(codeType, (MethodDeclaration)e); } if (e is Property) { PropertyEmitter.Emit(codeType, (e as Property)); } } }
// Emits expressions: in codedom terms, those that can't stand on their own. public static CodeExpression EmitCodeExpression(Expression expression) { if (expression is Literal) { return(LiteralEmitter.Emit((Literal)expression)); } if (expression is MethodInvocation) { return(MethodEmitter.Emit((MethodInvocation)expression)); } if (expression is VariableReference) { return(new CodeVariableReferenceExpression((expression as VariableReference).Name)); } if (expression is BinaryOperator) { return(OperatorEmitter.Emit((BinaryOperator)expression)); } if (expression is UnaryOperator) { return(OperatorEmitter.Emit(expression as UnaryOperator)); } if (expression is DirectionedArgument) { return(MethodEmitter.Emit(expression as DirectionedArgument)); } if (expression is Instantiation) { return(InstantiationEmitter.Emit(expression as Instantiation)); } if (expression is Value) { return(new CodeSnippetExpression("value")); } if (expression is IndexedIdentifier) { return(IndexedIdentifierEmitter.Emit(expression as IndexedIdentifier)); } throw new NotImplementedException(); }
/* Builds the CodeDOM statement for a class, and attaches it to a * CodeDOM namespace statement. */ public static void Emit(CodeNamespace codeNamespace, Class c) { // CodeTypeDeclaration is the CodeDOM representation of a // class, struct, or enum. var codeTypeDeclaration = new CodeTypeDeclaration(); codeNamespace.Types.Add(codeTypeDeclaration); // Assign the unqualified name (without namespace). codeTypeDeclaration.Name = c.UnqualifiedName; if (c.IsStruct) { codeTypeDeclaration.IsStruct = true; } else { // Flag the type as a class. codeTypeDeclaration.IsClass = true; } // Create the enum that sets attributes of the class. var typeAttr = TypeAttributes.Class; // If the class is "final", it is "sealed" in C# terms. if (c.IsFinal) { typeAttr |= TypeAttributes.Sealed; } if (c.IsAbstract) { typeAttr |= TypeAttributes.Abstract; } // Assign the partial state of the class. codeTypeDeclaration.IsPartial = c.IsPartial; // Add the list of generic type names of the class. foreach (string s in c.GenericTypeNames) { codeTypeDeclaration.TypeParameters.Add(new CodeTypeParameter(s)); } // Add the list of base type names of the class. foreach (string s in c.BaseTypeNames) { codeTypeDeclaration.BaseTypes.Add(s); } // Set the accessibility of the class. switch (c.Accessibility) { case Accessibility.Internal: typeAttr |= TypeAttributes.NestedAssembly; break; case Accessibility.Public: typeAttr |= TypeAttributes.Public; break; } codeTypeDeclaration.TypeAttributes = typeAttr; /* Iterate through and emit the children */ foreach (var e in c.ChildExpressions) { if (e is ClassVariable) { ClassVariableEmitter.Emit(codeTypeDeclaration, e as ClassVariable); } if (e is MethodDeclaration) { MethodEmitter.Emit(codeTypeDeclaration, (MethodDeclaration)e); } if (e is Constructor) { ConstructorEmitter.Emit(codeTypeDeclaration, (e as Constructor)); } if (e is Property) { PropertyEmitter.Emit(codeTypeDeclaration, (e as Property)); } if (e is Event) { EventEmitter.Emit(codeTypeDeclaration, (e as Event)); } } }