本文整理汇总了C#中System.CodeDom.CodeConstructor类的典型用法代码示例。如果您正苦于以下问题:C# CodeConstructor类的具体用法?C# CodeConstructor怎么用?C# CodeConstructor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeConstructor类属于System.CodeDom命名空间,在下文中一共展示了CodeConstructor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Process
public void Process(CodeTypeDeclaration typeDecl)
{
for (int i = 0; i < typeDecl.Members.Count; i++)
{
CodeTypeMember typeMember = typeDecl.Members[i];
if (typeMember is CodeMemberMethod)
{
CodeMemberMethod memberMethod = (CodeMemberMethod) typeMember;
CodeObjectSource source = Utils.GetTypeReferenceSource(memberMethod.ReturnType);
if (source.Target == typeof (void) && source.ArrayRanks.Length == 0 && memberMethod.Parameters.Count == 0)
{
if (StringUtils.CaseInsensitiveEquals(memberMethod.Name, "Class_Initialize"))
{
CodeConstructor constructor = new CodeConstructor();
constructor.Statements.AddRange(memberMethod.Statements);
typeDecl.Members[i] = constructor;
}
else if (StringUtils.CaseInsensitiveEquals(memberMethod.Name, "Class_Terminate"))
{
CodeDestructor destructor = new CodeDestructor();
destructor.Statements.AddRange(memberMethod.Statements);
typeDecl.Members[i] = destructor;
}
}
}
else if (typeMember is CodeTypeDeclaration)
{
Process((CodeTypeDeclaration) typeMember);
}
}
}
示例2: AddBodyParameterTest
public void AddBodyParameterTest()
{
var method = new MockMethod() { Name = "Method", Parameters = new Dictionary<string, IParameter>() };
var typeProvider = new DefaultObjectTypeProvider("Schema");
// Confirm that no body parameter is added.
var decorator = new RequestConstructorDecorator(typeProvider);
CodeConstructor constructor = new CodeConstructor();
method.HasBody = false;
decorator.AddBodyParameter(constructor, method);
Assert.AreEqual(0, constructor.Parameters.Count);
Assert.AreEqual(0, constructor.Statements.Count);
// Confirm that a required body parameter is added.
method.RequestType = "MySchema";
method.HasBody = true;
constructor = new CodeConstructor();
decorator.AddBodyParameter(constructor, method);
Assert.AreEqual(1, constructor.Parameters.Count);
Assert.AreEqual("body", constructor.Parameters[0].Name);
Assert.AreEqual("Schema.MySchema", constructor.Parameters[0].Type.BaseType);
Assert.AreEqual(1, constructor.Statements.Count);
}
示例3: CreateConstructor
internal CodeConstructor CreateConstructor(String serviceClassName, IResource resource)
{
var constructor = new CodeConstructor();
// public [ResourceClass]([ServiceClass] service, Google.Apis.Authentication.IAuthenticator authenticator)
constructor.Attributes = MemberAttributes.Public;
constructor.Parameters.Add(
new CodeParameterDeclarationExpression(serviceClassName, ResourceBaseGenerator.ServiceFieldName));
constructor.Parameters.Add(
new CodeParameterDeclarationExpression(typeof(Google.Apis.Authentication.IAuthenticator),
ResourceClassGenerator.AuthenticatorName));
// this.service = service
constructor.Statements.Add(
new CodeAssignStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), ResourceBaseGenerator.ServiceFieldName),
new CodeArgumentReferenceExpression(ResourceBaseGenerator.ServiceFieldName)));
// this.authenticator = authenticator
constructor.Statements.Add(
new CodeAssignStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), ResourceClassGenerator.AuthenticatorName),
new CodeArgumentReferenceExpression(ResourceClassGenerator.AuthenticatorName)));
// Initialize subresources
constructor.Statements.AddRange(CreateSubresourceCreateStatements(resource));
return constructor;
}
示例4: CreateServicesConstructor
protected virtual CodeConstructor CreateServicesConstructor()
{
CodeConstructor constructor = new CodeConstructor();
constructor.Attributes = MemberAttributes.Public;
constructor.Parameters.Add(new CodeParameterDeclarationExpression(_serviceType, _naming.ToVariableName(_serviceIdentifier)));
return constructor;
}
示例5: BuildCountCode
/// <summary>
/// This is a simple helper method to create a test valid
/// CodeObject to be feed into the
/// CreateScriptSource(CodeObject content, string id)
///
/// However, CodeObject is simply a base class so we probably
/// need a specific type of CodeObject but what?
///
/// [Bill - has indicate that CodeObject parameter for CreateScriptSource
/// does in fact need to be a CodeMemberMethod - Maybe a spec BUG]
///
/// Probably need to put this somewhere else maybe put this in:
/// ScriptEngineTestHelper.cs
/// </summary>
/// <returns>A valid CodeObject boxes some kind of CompileUnit</returns>
private static CodeObject BuildCountCode()
{
// Create a new CodeCompileUnit to contain
// the program graph.
CodeCompileUnit compileUnit = new CodeCompileUnit();
// Declare a new namespace called Samples.
CodeNamespace samples = new CodeNamespace("Samples");
// Add the new namespace to the compile unit.
compileUnit.Namespaces.Add(samples);
// Declare a new code entry point method.
CodeEntryPointMethod start = new CodeEntryPointMethod();
CodeConstructor codecon = new CodeConstructor();
//
CodeNamespace cns = new CodeNamespace("Test");
CodeTypeDeclaration ctd = new CodeTypeDeclaration("testclass");
ctd.IsClass = true;
CodeMemberMethod method = new CodeMemberMethod();
method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
method.Name = "AddTenToNumber";
method.ReturnType = new CodeTypeReference("Int32");
method.Parameters.Add(new CodeParameterDeclarationExpression("int", "number"));
method.Statements.Add(new CodeSnippetExpression("return number+10"));
ctd.Members.Add(method);
samples.Types.Add(ctd);
// If we return just the method this will not throw exception
// on CreateScriptSource
// return method;
return compileUnit;
}
示例6: CreateConstructor
private static CodeConstructor CreateConstructor(string className)
{
CodeConstructor result = new CodeConstructor();
result.Attributes = MemberAttributes.Public;
result.Name = className;
return result;
}
示例7: DeclareCodeType
public override void DeclareCodeType(IDLInterface idlIntf)
{
// Proxy class.
typeProxy = new CodeTypeDeclaration(name + "Proxy");
typeProxy.IsClass = true;
typeProxy.TypeAttributes = TypeAttributes.Public;
eventsDeclarationHolder = new CodeTypeDeferredNamespaceDeclarationHolderEvents(idlIntf);
typeProxy.BaseTypes.Add(genInterfaceName);
// Interface field.
CodeMemberField memberProxy = new CodeMemberField(genInterfaceName, proxyName);
memberProxy.Attributes = MemberAttributes.Private;
typeProxy.Members.Add(memberProxy); // TODO: Going to need a using or a fully qualified name.
// Constructor.
CodeConstructor constructor = new CodeConstructor();
constructor.Attributes = MemberAttributes.Public;
// TODO - use the actual interface type rather than a string.
paramProxy = new CodeParameterDeclarationExpression(genInterfaceName, proxyName);
constructor.Parameters.Add(paramProxy);
thisProxyFieldRef = new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), proxyName
);
assignProxy = new CodeAssignStatement(thisProxyFieldRef,
new CodeArgumentReferenceExpression(proxyName));
constructor.Statements.Add(assignProxy);
typeProxy.Members.Add(constructor);
declarationHolder = new CodeTypeIgnoredNamespaceDeclarationHolderParams(idlIntf);
contextDeclarationHolder = declarationHolder;
bAddNamespace = false;
}
示例8: 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));
}
示例9: GenerateCodeFromConstructor
/// <summary>
/// Generates code from the specified <paramref name="constructor"/>.
/// </summary>
/// <param name="constructor">Class constructor for which code needs to be generated.</param>
/// <param name="type">Type declaration.</param>
/// <param name="namespace">Namespace declaration.</param>
/// <param name="options">Code generation options.</param>
/// <remarks>
/// This method is a workaround for <see cref="CodeDomProvider.GenerateCodeFromMember"/>
/// not generating constructors properly.
/// </remarks>
private void GenerateCodeFromConstructor(
CodeConstructor constructor,
CodeTypeDeclaration type,
CodeNamespace @namespace,
CodeGeneratorOptions options)
{
const string StartMarker = "___startMarker___";
const string EndMarker = "___endMarker___";
// Insert marker fields around the target constructor
int indexOfMember = type.Members.IndexOf(constructor);
type.Members.Insert(indexOfMember + 1, new CodeMemberField(typeof(int), EndMarker));
type.Members.Insert(indexOfMember, new CodeMemberField(typeof(int), StartMarker));
using (StringWriter buffer = new StringWriter(CultureInfo.InvariantCulture))
{
// Generate type declaration in verbatim order to preserve placement of marker fields
options = options ?? new CodeGeneratorOptions();
options.VerbatimOrder = true;
this.LanguageProvider.GenerateCodeFromNamespace(@namespace, buffer, options);
// Extract constructor code from the generated type code
const string ConstructorCode = "constructor";
Regex regex = new Regex(
@"^[^\r\n]*" + StartMarker + @"[^\n]*$" +
@"(?<" + ConstructorCode + @">.*)" +
@"^[^\r\n]*" + EndMarker + @"[^\n]*$",
RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
string code = regex.Match(buffer.ToString()).Groups[ConstructorCode].Value;
// Write constructor code to the output buffer
this.ClassCode.Write(code);
}
}
示例10: Generate
public CodeExpression[] Generate(Table table, HardwireCodeGenerationContext generatorContext, CodeTypeMemberCollection members)
{
string className = "AIDX_" + Guid.NewGuid().ToString("N");
string name = table.Get("name").String;
bool setter = table.Get("setter").Boolean;
CodeTypeDeclaration classCode = new CodeTypeDeclaration(className);
classCode.TypeAttributes = System.Reflection.TypeAttributes.NestedPrivate | System.Reflection.TypeAttributes.Sealed;
classCode.BaseTypes.Add(typeof(ArrayMemberDescriptor));
CodeConstructor ctor = new CodeConstructor();
ctor.Attributes = MemberAttributes.Assembly;
classCode.Members.Add(ctor);
ctor.BaseConstructorArgs.Add(new CodePrimitiveExpression(name));
ctor.BaseConstructorArgs.Add(new CodePrimitiveExpression(setter));
DynValue vparams = table.Get("params");
if (vparams.Type == DataType.Table)
{
List<HardwireParameterDescriptor> paramDescs = HardwireParameterDescriptor.LoadDescriptorsFromTable(vparams.Table);
ctor.BaseConstructorArgs.Add(new CodeArrayCreateExpression(typeof(ParameterDescriptor), paramDescs.Select(e => e.Expression).ToArray()));
}
members.Add(classCode);
return new CodeExpression[] { new CodeObjectCreateExpression(className) };
}
示例11: GetInitializeMethod
protected override CodeMemberMethod GetInitializeMethod(IDesignerSerializationManager manager, CodeTypeDeclaration typeDecl, object value)
{
if (manager == null)
{
throw new ArgumentNullException("manager");
}
if (typeDecl == null)
{
throw new ArgumentNullException("typeDecl");
}
if (value == null)
{
throw new ArgumentNullException("value");
}
CodeMemberMethod method = typeDecl.UserData[_initMethodKey] as CodeMemberMethod;
if (method == null)
{
method = new CodeMemberMethod {
Name = "InitializeComponent",
Attributes = MemberAttributes.Private
};
typeDecl.UserData[_initMethodKey] = method;
CodeConstructor constructor = new CodeConstructor {
Attributes = MemberAttributes.Public
};
constructor.Statements.Add(new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "InitializeComponent", new CodeExpression[0]));
typeDecl.Members.Add(constructor);
}
return method;
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:30,代码来源:ActivityTypeCodeDomSerializer.cs
示例12: GenerateCode
public CodeCompileUnit GenerateCode(string typeName, string codeBody,
StringCollection imports,
string prefix)
{
var compileUnit = new CodeCompileUnit();
var typeDecl = new CodeTypeDeclaration(typeName);
typeDecl.IsClass = true;
typeDecl.TypeAttributes = TypeAttributes.Public;
// create constructor
var constructMember = new CodeConstructor {Attributes = MemberAttributes.Public};
typeDecl.Members.Add(constructMember);
// pump in the user specified code as a snippet
var literalMember =
new CodeSnippetTypeMember(codeBody);
typeDecl.Members.Add(literalMember);
var nspace = new CodeNamespace();
////Add default imports
//foreach (string nameSpace in ScriptExecuter._namespaces)
//{
// nspace.Imports.Add(new CodeNamespaceImport(nameSpace));
//}
foreach (string nameSpace in imports)
{
nspace.Imports.Add(new CodeNamespaceImport(nameSpace));
}
compileUnit.Namespaces.Add(nspace);
nspace.Types.Add(typeDecl);
return compileUnit;
}
示例13: GetInitializeMethod
protected override CodeMemberMethod GetInitializeMethod(IDesignerSerializationManager manager, CodeTypeDeclaration typeDecl, object value)
{
if (manager == null)
throw new ArgumentNullException("manager");
if (typeDecl == null)
throw new ArgumentNullException("typeDecl");
if (value == null)
throw new ArgumentNullException("value");
CodeMemberMethod method = typeDecl.UserData[_initMethodKey] as CodeMemberMethod;
if (method == null)
{
method = new CodeMemberMethod();
method.Name = _initMethodName;
method.Attributes = MemberAttributes.Private;
typeDecl.UserData[_initMethodKey] = method;
// Now create a ctor that calls this method.
CodeConstructor ctor = new CodeConstructor();
ctor.Attributes = MemberAttributes.Public;
ctor.Statements.Add(new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), _initMethodName));
typeDecl.Members.Add(ctor);
}
return method;
}
示例14: 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);
}
示例15: AddConstructor
/// <summary>
/// Add a constructor to the class.
/// </summary>
public void AddConstructor()
{
// Declare the constructor
CodeConstructor constructor = new CodeConstructor();
constructor.Attributes =
MemberAttributes.Public | MemberAttributes.Final;
// Add parameters.
constructor.Parameters.Add(new CodeParameterDeclarationExpression(
typeof(System.Double), "width"));
constructor.Parameters.Add(new CodeParameterDeclarationExpression(
typeof(System.Double), "height"));
// Add field initialization logic
CodeFieldReferenceExpression widthReference =
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), "widthValue");
constructor.Statements.Add(new CodeAssignStatement(widthReference,
new CodeArgumentReferenceExpression("width")));
CodeFieldReferenceExpression heightReference =
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), "heightValue");
constructor.Statements.Add(new CodeAssignStatement(heightReference,
new CodeArgumentReferenceExpression("height")));
targetClass.Members.Add(constructor);
}