本文整理汇总了C#中System.CodeDom.CodeTypeDeclaration类的典型用法代码示例。如果您正苦于以下问题:C# CodeTypeDeclaration类的具体用法?C# CodeTypeDeclaration怎么用?C# CodeTypeDeclaration使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeTypeDeclaration类属于System.CodeDom命名空间,在下文中一共展示了CodeTypeDeclaration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CustomizeCodeDom
public void CustomizeCodeDom(CodeCompileUnit codeUnit, IServiceProvider services)
{
var types = codeUnit.Namespaces[0].Types;
var attributes = new HashSet<string>();
foreach (var type in types.Cast<CodeTypeDeclaration>().
Where(type => type.IsClass && !type.IsContextType()))
{
attributes.Clear();
var @struct = new CodeTypeDeclaration {
Name = AttributeConstsStructName,
IsStruct = true,
TypeAttributes = TypeAttributes.Public
};
foreach (var member in from CodeTypeMember member in type.Members
let prop = member as CodeMemberProperty
where prop != null
select prop)
{
CreateAttributeConstForProperty(@struct, member, attributes);
}
if (attributes.Any())
{
type.Members.Insert(0, GenerateTypeWithoutEmptyLines(@struct));
}
}
}
示例2: SetTestFixtureSetup
public void SetTestFixtureSetup(CodeMemberMethod fixtureSetupMethod)
{
// xUnit uses IUseFixture<T> on the class
fixtureSetupMethod.Attributes |= MemberAttributes.Static;
_currentFixtureTypeDeclaration = new CodeTypeDeclaration("FixtureData");
_currentTestTypeDeclaration.Members.Add(_currentFixtureTypeDeclaration);
var fixtureDataType =
CodeDomHelper.CreateNestedTypeReference(_currentTestTypeDeclaration, _currentFixtureTypeDeclaration.Name);
var useFixtureType = new CodeTypeReference(IUSEFIXTURE_INTERFACE, fixtureDataType);
CodeDomHelper.SetTypeReferenceAsInterface(useFixtureType);
_currentTestTypeDeclaration.BaseTypes.Add(useFixtureType);
// public void SetFixture(T) { } // explicit interface implementation for generic interfaces does not work with codedom
CodeMemberMethod setFixtureMethod = new CodeMemberMethod();
setFixtureMethod.Attributes = MemberAttributes.Public;
setFixtureMethod.Name = "SetFixture";
setFixtureMethod.Parameters.Add(new CodeParameterDeclarationExpression(fixtureDataType, "fixtureData"));
setFixtureMethod.ImplementationTypes.Add(useFixtureType);
_currentTestTypeDeclaration.Members.Add(setFixtureMethod);
// public <_currentFixtureTypeDeclaration>() { <fixtureSetupMethod>(); }
CodeConstructor ctorMethod = new CodeConstructor();
ctorMethod.Attributes = MemberAttributes.Public;
_currentFixtureTypeDeclaration.Members.Add(ctorMethod);
ctorMethod.Statements.Add(
new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression(new CodeTypeReference(_currentTestTypeDeclaration.Name)),
fixtureSetupMethod.Name));
}
示例3: GenerateContract
/// <summary>Modifies the code document object model prior to the contract generation process.</summary>
/// <param name="context">The code generated context to use to modify the code document prior to generation.</param>
public void GenerateContract(ServiceContractGenerationContext context)
{
// Disable generation of the Event-Based Async Pattern, which has a conflicting naming scheme.
context.ServiceContractGenerator.Options &= ~ServiceContractGenerationOptions.EventBasedAsynchronousMethods;
string contractName = context.Contract.Name;
string clientTypeName = TaskAsyncWsdlImportExtension.DeriveClientTypeName(contractName);
// Look up the client class, and create it if it doesn't already exist.
if (TaskAsyncWsdlImportExtension.FindClientType(clientTypeName, context.ServiceContractGenerator.TargetCompileUnit.Namespaces) == null)
{
// Create the new type
CodeTypeDeclaration newClient = new CodeTypeDeclaration(clientTypeName)
{
Attributes = MemberAttributes.Public,
IsPartial = true
};
newClient.BaseTypes.Add(new CodeTypeReference(typeof(ClientBase<>)) { TypeArguments = { new CodeTypeReference(contractName) } });
newClient.BaseTypes.Add(new CodeTypeReference(contractName));
// Add the new type to the right namespace
CodeNamespace contractNamespace = (from ns in context.ServiceContractGenerator.TargetCompileUnit.Namespaces.Cast<CodeNamespace>()
from type in ns.Types.Cast<CodeTypeDeclaration>()
where type == context.ContractType
select ns).FirstOrDefault();
contractNamespace.Types.Add(newClient);
}
}
示例4: BuildProgram
static CodeNamespace BuildProgram()
{
// namespaceの作成
var ns = new CodeNamespace("MetaWorld");
// import編成
var systemImport = new CodeNamespaceImport("System");
// class作成
var programClass = new CodeTypeDeclaration("Program");
// mainメソッドの定義
var methodMain = new CodeMemberMethod()
{
Attributes = MemberAttributes.Static,
Name = "Main"
};
methodMain.Statements.Add(
new CodeMethodInvokeExpression(
new CodeSnippetExpression("Console"),
"WriteLine",
new CodePrimitiveExpression("Hello World")
)
);
// コード構造の編成
programClass.Members.Add(methodMain);
ns.Imports.Add(systemImport);
ns.Types.Add(programClass);
return ns;
}
示例5: ProcessGeneratedCode
public override void ProcessGeneratedCode(CodeCompileUnit codeCompileUnit, CodeTypeDeclaration baseType, CodeTypeDeclaration derivedType, CodeMemberMethod buildMethod, CodeMemberMethod dataBindingMethod)
{
if (!String.IsNullOrWhiteSpace(Inherits))
{
derivedType.BaseTypes[0] = new CodeTypeReference(Inherits);
}
}
示例6: AddConstructor
private static void AddConstructor(CodeTypeDeclaration declaration)
{
CodeConstructor constructor = new CodeConstructor();
constructor.Attributes = MemberAttributes.Public;
constructor.Parameters.Add(
new CodeParameterDeclarationExpression(
typeof(IDbConnection), "connection"));
constructor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("connection"));
constructor.CustomAttributes.Add(new CodeAttributeDeclaration(
new CodeTypeReference(typeof(DebuggerNonUserCodeAttribute))
));
constructor.Statements.Add(
new CodeAssignStatement(
new CodeVariableReferenceExpression(SqlDataContextHelperClassName),
new CodeObjectCreateExpression(
typeof(SqlDataContextHelperClass),
new CodeExpression[] {
new CodeThisReferenceExpression()
}
)
));
declaration.Members.Add(constructor);
}
示例7: GenerateParameterProperty
internal CodeTypeMemberCollection GenerateParameterProperty(IParameter parameter,
IMethod method,
CodeTypeDeclaration resourceClass,
IEnumerable<string> usedNames)
{
// Get the name and return type of this parameter.
string name = parameter.Name;
CodeTypeReference returnType = ResourceBaseGenerator.GetParameterTypeReference(
resourceClass, parameter);
// Generate the property and field.
CodeTypeMemberCollection newMembers = DecoratorUtil.CreateAutoProperty(
name, parameter.Description, returnType, usedNames, parameter.IsRequired);
// Add the KeyAttribute to the property.
foreach (CodeTypeMember member in newMembers)
{
CodeMemberProperty property = member as CodeMemberProperty;
if (property == null)
{
continue;
}
// Declare the RequestParameter attribute.
CodeTypeReference attributeType = new CodeTypeReference(typeof(RequestParameterAttribute));
CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(attributeType);
attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(parameter.Name)));
property.CustomAttributes.Add(attribute);
}
return newMembers;
}
示例8: Correct
public void Correct(CodeTypeDeclaration typeDecl)
{
foreach (CodeTypeMember typeMember in typeDecl.Members)
{
if (typeMember is CodeMemberMethod)
{
CodeMemberMethod memberMethod = (CodeMemberMethod) typeMember;
CodeObjectSource source = Utils.GetTypeReferenceSource(memberMethod.ReturnType);
if (source == null || source.Target != typeof (void) || source.ArrayRanks.Length > 0)
{
CorrectStatements(memberMethod.Name, memberMethod.Statements, memberMethod.ReturnType);
}
}
else if (typeMember is CodeMemberProperty)
{
CodeMemberProperty memberProperty = (CodeMemberProperty) typeMember;
if (memberProperty.HasGet)
{
CorrectStatements(memberProperty.Name, memberProperty.GetStatements, memberProperty.Type);
}
if (memberProperty.HasSet)
{
ProcessSetStatements(memberProperty.Name, memberProperty.SetStatements);
}
}
else if (typeMember is CodeTypeDeclaration)
{
Correct((CodeTypeDeclaration) typeMember);
}
}
}
示例9: CreatePartialClass
static CodeTypeDeclaration CreatePartialClass (SteticCompilationUnit globalUnit, List<SteticCompilationUnit> units, GenerationOptions options, string name)
{
SteticCompilationUnit unit;
if (options.GenerateSingleFile)
unit = globalUnit;
else {
unit = new SteticCompilationUnit (name);
units.Add (unit);
}
string ns = "";
int i = name.LastIndexOf ('.');
if (i != -1) {
ns = name.Substring (0, i);
name = name.Substring (i+1);
}
CodeTypeDeclaration type = new CodeTypeDeclaration (name);
type.IsPartial = true;
type.Attributes = MemberAttributes.Public;
type.TypeAttributes = TypeAttributes.Public;
CodeNamespace cns = new CodeNamespace (ns);
cns.Types.Add (type);
unit.Namespaces.Add (cns);
return type;
}
示例10: CodeDomEntity
public CodeDomEntity(JToken structualType, string targetNamespace)
{
this.targetNamespace = targetNamespace;
outputFileName = string.Format("{0}.cs", structualType["shortName"].Value<string>());
this.structualType = structualType;
targetUnit = new CodeCompileUnit();
CodeNamespace breezeEntities = new CodeNamespace(targetNamespace);
breezeEntities.Imports.Add(new CodeNamespaceImport("System"));
breezeEntities.Imports.Add(new CodeNamespaceImport("Breeze.Sharp"));
breezeEntities.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
targetClass = new CodeTypeDeclaration(structualType["shortName"].Value<string>());
if (structualType["baseTypeName"] != null)
{
//use base class from the base type (it will still be BaseEntity)
string baseType = structualType["baseTypeName"].Value<string>();
targetClass.BaseTypes.Add(new CodeTypeReference(baseType.Substring(0, baseType.IndexOf(':'))));
}
else
{
//this entity type has no base class so use BaseEntity
targetClass.BaseTypes.Add(new CodeTypeReference("BaseEntity"));
}
targetClass.IsClass = true;
targetClass.TypeAttributes = TypeAttributes.Public;
breezeEntities.Types.Add(targetClass);
targetUnit.Namespaces.Add(breezeEntities);
}
示例11: CreateClass
private CodeTypeDeclaration CreateClass(ClassDefinition table)
{
CodeTypeDeclaration type = new CodeTypeDeclaration();
type.Name = table.ToString();
type.IsClass = true;
type.IsPartial = true;
type.BaseTypes.Add(new CodeTypeReference(typeof(CsDO.Lib.DataObject)));
type.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable",
new CodeAttributeArgument[] { }));
if (!String.IsNullOrEmpty(table.Alias) && !table.Table.Equals(table.Alias))
{
type.CustomAttributes.Add(new CodeAttributeDeclaration("Table",
new CodeAttributeArgument[] {
new CodeAttributeArgument(new CodePrimitiveExpression(table.Table))
}));
}
#region class comments
type.Comments.AddRange(InsertDocumentation(Documetation.Remarks, new string[]
{
"Persistence class that maps table '" + table + "'",
"Warning: Each property maps a column, use the attribute",
"Column to mark properties that should not be persisted."
}));
type.Comments.AddRange(InsertDocumentation(Documetation.SeeAlso, new string[] { "Column" }));
#endregion
return type;
}
示例12: Generate
/// <summary>
/// Generates code for value
/// </summary>
/// <param name="parentClass">The parent class.</param>
/// <param name="method">The method.</param>
/// <param name="value">The value.</param>
/// <param name="baseName">Name of the base.</param>
/// <param name="dictionary">The dictionary.</param>
/// <returns></returns>
public CodeExpression Generate(CodeTypeDeclaration parentClass, CodeMemberMethod method, object value, string baseName, ResourceDictionary dictionary = null)
{
string bitmapVarName = baseName + "_bm";
BitmapImage bitmap = value as BitmapImage;
return CodeComHelper.GenerateBitmapImageValue(method, bitmap.UriSource, bitmapVarName);
}
示例13: BuildTest
public void BuildTest()
{
var typeMember = new CodeMemberMethod() { Name = "TypeMemberTest" };
var propData = mocks.Stub<IBuilderData>();
Expect.Call(buildcontext.TypeMember).Return(typeMember);
//Expect.Call(buildcontext.GetBuilderData("Property")).Return(propData);
// is not relevant ... just a devel-testing call to
// "var userData = context.GetBuilderData<BuildParametersOfPropertyBuilder>(this);"
Expect.Call(buildcontext.GetBuilderData<BuildParametersOfPropertyBuilder>(testObject)).Return(null);
Expect.Call(buildcontext.GetBuilderData("Property")).Return(propData).Repeat.Any();
var testClass = new CodeTypeDeclaration("TheClass");
Expect.Call(buildcontext.TestClassDeclaration).Return(testClass).Repeat.Any();
//Expect.Call(buildcontext.IsProperty).Return(true);
mocks.ReplayAll();
testObject.Build(this.buildcontext);
// Todo: check if only the ending "Test" gets replaced.
var expected = "TypeMemberNormalBehavior";
var actual = typeMember.Name;
Assert.AreEqual(expected, actual);
mocks.VerifyAll();
}
示例14: CreateClass
public CodeTypeDeclaration CreateClass()
{
CodeTypeDeclaration declaration = new CodeTypeDeclaration(_helperClassName);
declaration.IsClass = true;
declaration.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed;
declaration.BaseTypes.Add(typeof(DataProviderHelperBase));
declaration.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(typeof(EditorBrowsableAttribute)),
new CodeAttributeArgument(
new CodeFieldReferenceExpression(
new CodeTypeReferenceExpression(typeof(EditorBrowsableState)),
EditorBrowsableState.Never.ToString()
)
)
)
);
AddConstructor(declaration);
AddInterfaceTypeProperty(declaration);
AddDataIdTypeProperty(declaration);
AddCreateDataIdFunctionMethod(declaration);
AddValidateDataTypeMethod(declaration);
AddCreateNewElementMethod(declaration);
return declaration;
}
示例15: CodeSnippetBlankLines
public void CodeSnippetBlankLines ()
{
var opt = new CodeGeneratorOptions () {
BlankLinesBetweenMembers = false,
VerbatimOrder = false
};
var ccu = new CodeCompileUnit ();
var ns = new CodeNamespace ("Foo");
ccu.Namespaces.Add (ns);
var t = new CodeTypeDeclaration ("Bar");
ns.Types.Add (t);
t.Members.Add (new CodeSnippetTypeMember ("#line hidden"));
t.Members.Add (new CodeSnippetTypeMember ("#line hidden2"));
t.Members.Add (new CodeMemberMethod () { Name = "Foo" });
using (var sw = new StringWriter ()) {
new CSharpCodeProvider ().GenerateCodeFromCompileUnit (ccu, sw, opt);
var str = sw.ToString ();
Assert.IsFalse (str.Contains ("hidden2private"), "#0");
Assert.IsTrue (str.Contains( "#line hidden#line hidden2"), "#1");
}
}