本文整理汇总了C#中CodeTypeReference类的典型用法代码示例。如果您正苦于以下问题:C# CodeTypeReference类的具体用法?C# CodeTypeReference怎么用?C# CodeTypeReference使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeTypeReference类属于命名空间,在下文中一共展示了CodeTypeReference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Ctor_String_CodeTypeReference_ParamsCodeStatement
public void Ctor_String_CodeTypeReference_ParamsCodeStatement(string localName, CodeTypeReference catchExceptionType, CodeStatement[] statements)
{
var catchClause = new CodeCatchClause(localName, catchExceptionType, statements);
Assert.Equal(localName ?? string.Empty, catchClause.LocalName);
Assert.Equal((catchExceptionType ?? new CodeTypeReference(typeof(Exception))).BaseType, catchClause.CatchExceptionType.BaseType);
Assert.Equal(statements, catchClause.Statements.Cast<CodeStatement>());
}
示例2: CodeCatchClause
public CodeCatchClause(string localName, CodeTypeReference catchExceptionType, params CodeStatement[] statements)
: this()
{
LocalName = localName;
CatchExceptionType = catchExceptionType;
Statements.AddRange(statements);
}
示例3: GenSymAutomatic
public CodeVariableReferenceExpression GenSymAutomatic(string prefix, CodeTypeReference type, bool parameter)
{
int i = 1;
while (autos.Select(l => l.Key).Contains(prefix + i))
++i;
EnsureLocalVariable(prefix + i, type, parameter);
return new CodeVariableReferenceExpression(prefix + i);
}
示例4: CodeAttributeDeclaration
public CodeAttributeDeclaration(CodeTypeReference attributeType, params CodeAttributeArgument[] arguments) {
this.attributeType = attributeType;
if( attributeType != null) {
this.name = attributeType.BaseType;
}
if(arguments != null) {
Arguments.AddRange(arguments);
}
}
示例5: TypeEquals
public static bool TypeEquals(this CodeTypeReference self, CodeTypeReference other)
{
if (self.BaseType != other.BaseType || self.TypeArguments.Count != other.TypeArguments.Count)
return false;
for (int i = 0; i < self.TypeArguments.Count; i++) {
if (!self.TypeArguments[i].TypeEquals(other.TypeArguments[i]))
return false;
}
return true;
}
示例6: CppTypeToCodeDomType
// Return the CodeDom type reference corresponding to T, or null
public CodeTypeReference CppTypeToCodeDomType(CppType t, out bool byref)
{
CodeTypeReference rtype = null;
byref = false;
Type mtype = t.ToManagedType ();
if (mtype != null) {
if (mtype.IsByRef) {
byref = true;
mtype = mtype.GetElementType ();
}
return new CodeTypeReference (mtype);
}
if (t.Modifiers.Count > 0 && t.ElementType != CppTypes.Void && t.ElementType != CppTypes.Class)
return null;
switch (t.ElementType) {
case CppTypes.Void:
if (t.Modifiers.Count > 0) {
if (t.Modifiers.Count == 1 && t.Modifiers [0] == CppModifiers.Pointer)
rtype = new CodeTypeReference (typeof (IntPtr));
else
return null;
} else {
rtype = new CodeTypeReference (typeof (void));
}
break;
case CppTypes.Bool:
rtype = new CodeTypeReference (typeof (bool));
break;
case CppTypes.Int:
rtype = new CodeTypeReference (typeof (int));
break;
case CppTypes.Float:
rtype = new CodeTypeReference (typeof (float));
break;
case CppTypes.Double:
rtype = new CodeTypeReference (typeof (double));
break;
case CppTypes.Char:
rtype = new CodeTypeReference (typeof (char));
break;
case CppTypes.Class:
// FIXME: Full name
rtype = new CodeTypeReference (t.ElementTypeName);
break;
default:
return null;
}
return rtype;
}
示例7: Ctor_CodeTypeReference
public void Ctor_CodeTypeReference(CodeTypeReference attributeType, CodeAttributeArgument[] arguments)
{
if (arguments == null || arguments.Length == 0)
{
var declaration1 = new CodeAttributeDeclaration(attributeType);
Assert.Equal(attributeType?.BaseType ?? string.Empty, declaration1.Name);
Assert.Equal(attributeType, declaration1.AttributeType);
Assert.Empty(declaration1.Arguments);
}
var declaration2 = new CodeAttributeDeclaration(attributeType, arguments);
Assert.Equal(attributeType?.BaseType ?? string.Empty, declaration2.Name);
Assert.Equal(attributeType, declaration2.AttributeType);
Assert.Equal(arguments ?? new CodeAttributeArgument[0], declaration2.Arguments.Cast<CodeAttributeArgument>());
}
示例8: CodeTypeReference
public CodeTypeReference(Type type) {
if (type == null)
throw new ArgumentNullException("type");
if (type.IsArray) {
this.arrayRank = type.GetArrayRank();
this.arrayElementType = new CodeTypeReference(type.GetElementType());
this.baseType = null;
} else {
InitializeFromType(type);
this.arrayRank = 0;
this.arrayElementType = null;
}
this.isInterface = type.IsInterface;
}
示例9: FormatComment
public static void FormatComment(string docs, CodeTypeMember cmp, bool obsolete = false, string tag = "summary")
{
StringBuilder obsoleteMessageBuilder = new StringBuilder();
cmp.Comments.Add(new CodeCommentStatement(string.Format("<{0}>", tag), true));
foreach (string line in HtmlEncoder.HtmlEncode(docs).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.None))
{
cmp.Comments.Add(new CodeCommentStatement(string.Format("<para>{0}</para>", line), true));
if (obsolete && (line.Contains("instead") || line.Contains("deprecated")))
{
obsoleteMessageBuilder.Append(HtmlEncoder.HtmlDecode(line));
obsoleteMessageBuilder.Append(' ');
}
}
cmp.Comments.Add(new CodeCommentStatement(string.Format("</{0}>", tag), true));
if (obsoleteMessageBuilder.Length > 0)
{
obsoleteMessageBuilder.Remove(obsoleteMessageBuilder.Length - 1, 1);
CodeTypeReference obsoleteAttribute = new CodeTypeReference(typeof(ObsoleteAttribute));
CodePrimitiveExpression obsoleteMessage = new CodePrimitiveExpression(obsoleteMessageBuilder.ToString());
cmp.CustomAttributes.Add(new CodeAttributeDeclaration(obsoleteAttribute, new CodeAttributeArgument(obsoleteMessage)));
}
}
示例10: CodeTypeReference
public CodeTypeReference()
{
this.baseType = string.Empty;
this.arrayRank = 0;
this.arrayElementType = null;
}
示例11: GenericTypesAndConstraints
public void GenericTypesAndConstraints()
{
CodeNamespace ns = new CodeNamespace("NS");
ns.Imports.Add(new CodeNamespaceImport("System"));
ns.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
CodeTypeDeclaration class1 = new CodeTypeDeclaration();
class1.Name = "MyDictionary";
class1.BaseTypes.Add(new CodeTypeReference("Dictionary", new CodeTypeReference[] { new CodeTypeReference("TKey"), new CodeTypeReference("TValue"), }));
CodeTypeParameter kType = new CodeTypeParameter("TKey");
kType.HasConstructorConstraint = true;
kType.Constraints.Add(new CodeTypeReference(typeof(IComparable)));
kType.CustomAttributes.Add(new CodeAttributeDeclaration(
"System.ComponentModel.DescriptionAttribute", new CodeAttributeArgument(new CodePrimitiveExpression("KeyType"))));
CodeTypeReference iComparableT = new CodeTypeReference("IComparable");
iComparableT.TypeArguments.Add(new CodeTypeReference(kType));
kType.Constraints.Add(iComparableT);
CodeTypeParameter vType = new CodeTypeParameter("TValue");
vType.Constraints.Add(new CodeTypeReference(typeof(IList<System.String>)));
vType.CustomAttributes.Add(new CodeAttributeDeclaration(
"System.ComponentModel.DescriptionAttribute", new CodeAttributeArgument(new CodePrimitiveExpression("ValueType"))));
class1.TypeParameters.Add(kType);
class1.TypeParameters.Add(vType);
ns.Types.Add(class1);
// Declare a generic method.
CodeMemberMethod printMethod = new CodeMemberMethod();
CodeTypeParameter sType = new CodeTypeParameter("S");
sType.HasConstructorConstraint = true;
CodeTypeParameter tType = new CodeTypeParameter("T");
sType.HasConstructorConstraint = true;
printMethod.Name = "Nop";
printMethod.TypeParameters.Add(sType);
printMethod.TypeParameters.Add(tType);
printMethod.Attributes = MemberAttributes.Public;
class1.Members.Add(printMethod);
var class2 = new CodeTypeDeclaration();
class2.Name = "Demo";
var methodMain = new CodeEntryPointMethod();
var myClass = new CodeTypeReference(
"MyDictionary",
new CodeTypeReference[] {
new CodeTypeReference(typeof(int)),
new CodeTypeReference("List",
new CodeTypeReference[]
{new CodeTypeReference("System.String") })});
methodMain.Statements.Add(new CodeVariableDeclarationStatement(myClass, "dict", new CodeObjectCreateExpression(myClass)));
string dictionaryTypeName = typeof(System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<string>>[]).FullName;
var dictionaryType = new CodeTypeReference(dictionaryTypeName);
methodMain.Statements.Add(
new CodeVariableDeclarationStatement(dictionaryType, "dict2",
new CodeArrayCreateExpression(dictionaryType, new CodeExpression[1] { new CodePrimitiveExpression(null) })));
class2.Members.Add(methodMain);
ns.Types.Add(class2);
AssertEqual(ns,
@"Imports System
Imports System.Collections.Generic
Namespace NS
Public Class MyDictionary(Of TKey As {System.IComparable, IComparable(Of TKey), New}, TValue As System.Collections.Generic.IList(Of String))
Inherits Dictionary(Of TKey, TValue)
Public Overridable Sub Nop(Of S As New, T)()
End Sub
End Class
Public Class Demo
Public Shared Sub Main()
Dim dict As MyDictionary(Of Integer, List(Of String)) = New MyDictionary(Of Integer, List(Of String))()
Dim dict2() As System.Collections.Generic.Dictionary(Of Integer, System.Collections.Generic.List(Of String)) = New System.Collections.Generic.Dictionary(Of Integer, System.Collections.Generic.List(Of String))() {Nothing}
End Sub
End Class
End Namespace");
}
示例12: MetadataAttributes
public void MetadataAttributes()
{
var cu = new CodeCompileUnit();
var ns = new CodeNamespace();
ns.Name = "MyNamespace";
ns.Imports.Add(new CodeNamespaceImport("System"));
ns.Imports.Add(new CodeNamespaceImport("System.Drawing"));
ns.Imports.Add(new CodeNamespaceImport("System.Windows.Forms"));
ns.Imports.Add(new CodeNamespaceImport("System.ComponentModel"));
cu.Namespaces.Add(ns);
var attrs = cu.AssemblyCustomAttributes;
attrs.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyTitle", new CodeAttributeArgument(new CodePrimitiveExpression("MyAssembly"))));
attrs.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyVersion", new CodeAttributeArgument(new CodePrimitiveExpression("1.0.6.2"))));
attrs.Add(new CodeAttributeDeclaration("System.CLSCompliantAttribute", new CodeAttributeArgument(new CodePrimitiveExpression(false))));
var class1 = new CodeTypeDeclaration() { Name = "MyClass" };
class1.CustomAttributes.Add(new CodeAttributeDeclaration("System.Serializable"));
class1.CustomAttributes.Add(new CodeAttributeDeclaration("System.Obsolete", new CodeAttributeArgument(new CodePrimitiveExpression("Don't use this Class"))));
ns.Types.Add(class1);
var nestedClass = new CodeTypeDeclaration("NestedClass") { IsClass = true, TypeAttributes = TypeAttributes.NestedPublic };
nestedClass.CustomAttributes.Add(new CodeAttributeDeclaration("System.Serializable"));
class1.Members.Add(nestedClass);
var method1 = new CodeMemberMethod() { Name = "MyMethod" };
method1.CustomAttributes.Add(new CodeAttributeDeclaration("System.Obsolete", new CodeAttributeArgument(new CodePrimitiveExpression("Don't use this Method"))));
method1.CustomAttributes.Add(new CodeAttributeDeclaration("System.ComponentModel.Editor", new CodeAttributeArgument(new CodePrimitiveExpression("This")), new CodeAttributeArgument(new CodePrimitiveExpression("That"))));
var param1 = new CodeParameterDeclarationExpression(typeof(string), "blah");
param1.CustomAttributes.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlElementAttribute",
new CodeAttributeArgument("Form", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.Xml.Schema.XmlSchemaForm"), "Unqualified")),
new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(false))));
method1.Parameters.Add(param1);
var param2 = new CodeParameterDeclarationExpression(typeof(int[]), "arrayit");
param2.CustomAttributes.Add(
new CodeAttributeDeclaration("System.Xml.Serialization.XmlElementAttribute",
new CodeAttributeArgument("Form", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.Xml.Schema.XmlSchemaForm"), "Unqualified")),
new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(false))));
method1.Parameters.Add(param2);
class1.Members.Add(method1);
var function1 = new CodeMemberMethod();
function1.Name = "MyFunction";
function1.ReturnType = new CodeTypeReference(typeof(string));
function1.CustomAttributes.Add(new CodeAttributeDeclaration("System.Obsolete", new CodeAttributeArgument(new CodePrimitiveExpression("Don't use this Function"))));
function1.ReturnTypeCustomAttributes.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlIgnoreAttribute"));
function1.ReturnTypeCustomAttributes.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlRootAttribute", new
CodeAttributeArgument("Namespace", new CodePrimitiveExpression("Namespace Value")), new
CodeAttributeArgument("ElementName", new CodePrimitiveExpression("Root, hehehe"))));
function1.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression("Return")));
class1.Members.Add(function1);
CodeMemberMethod function2 = new CodeMemberMethod();
function2.Name = "GlobalKeywordFunction";
function2.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(ObsoleteAttribute), CodeTypeReferenceOptions.GlobalReference), new
CodeAttributeArgument(new CodePrimitiveExpression("Don't use this Function"))));
CodeTypeReference typeRef = new CodeTypeReference("System.Xml.Serialization.XmlIgnoreAttribute", CodeTypeReferenceOptions.GlobalReference);
CodeAttributeDeclaration codeAttrib = new CodeAttributeDeclaration(typeRef);
function2.ReturnTypeCustomAttributes.Add(codeAttrib);
class1.Members.Add(function2);
CodeMemberField field1 = new CodeMemberField();
field1.Name = "myField";
field1.Type = new CodeTypeReference(typeof(string));
field1.CustomAttributes.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlElementAttribute"));
field1.InitExpression = new CodePrimitiveExpression("hi!");
class1.Members.Add(field1);
CodeMemberProperty prop1 = new CodeMemberProperty();
prop1.Name = "MyProperty";
prop1.Type = new CodeTypeReference(typeof(string));
prop1.CustomAttributes.Add(new CodeAttributeDeclaration("System.Obsolete", new CodeAttributeArgument(new CodePrimitiveExpression("Don't use this Property"))));
prop1.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "myField")));
class1.Members.Add(prop1);
CodeConstructor const1 = new CodeConstructor();
const1.CustomAttributes.Add(new CodeAttributeDeclaration("System.Obsolete", new CodeAttributeArgument(new CodePrimitiveExpression("Don't use this Constructor"))));
class1.Members.Add(const1);
class1 = new CodeTypeDeclaration("Test");
class1.IsClass = true;
class1.BaseTypes.Add(new CodeTypeReference("Form"));
ns.Types.Add(class1);
CodeMemberField mfield = new CodeMemberField(new CodeTypeReference("Button"), "b");
mfield.InitExpression = new CodeObjectCreateExpression(new CodeTypeReference("Button"));
class1.Members.Add(mfield);
CodeConstructor ctor = new CodeConstructor();
ctor.Attributes = MemberAttributes.Public;
ctor.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),
"Size"), new CodeObjectCreateExpression(new CodeTypeReference("Size"),
new CodePrimitiveExpression(600), new CodePrimitiveExpression(600))));
ctor.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("b"),
"Text"), new CodePrimitiveExpression("Test")));
ctor.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("b"),
"TabIndex"), new CodePrimitiveExpression(0)));
ctor.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("b"),
"Location"), new CodeObjectCreateExpression(new CodeTypeReference("Point"),
//.........这里部分代码省略.........
示例13: GenSymParameter
public CodeVariableReferenceExpression GenSymParameter(string prefix, CodeTypeReference type)
{
return GenSymAutomatic(prefix, type, true);
}
示例14: GlobalKeyword
public void GlobalKeyword()
{
CodeNamespace ns = new CodeNamespace("Foo");
ns.Comments.Add(new CodeCommentStatement("Foo namespace"));
CodeTypeDeclaration cd = new CodeTypeDeclaration("Foo");
ns.Types.Add(cd);
string fieldName1 = "_verifyGlobalGeneration1";
CodeMemberField field = new CodeMemberField();
field.Name = fieldName1;
field.Type = new CodeTypeReference(typeof(int), CodeTypeReferenceOptions.GlobalReference);
field.Attributes = MemberAttributes.Public;
field.InitExpression = new CodePrimitiveExpression(int.MaxValue);
cd.Members.Add(field);
string fieldName2 = "_verifyGlobalGeneration2";
CodeMemberField field2 = new CodeMemberField();
field2.Name = fieldName2;
CodeTypeReference typeRef = new CodeTypeReference("System.Nullable", CodeTypeReferenceOptions.GlobalReference);
typeRef.TypeArguments.Add(new CodeTypeReference(typeof(int), CodeTypeReferenceOptions.GlobalReference));
field2.Type = typeRef;
field2.InitExpression = new CodePrimitiveExpression(0);
cd.Members.Add(field2);
CodeMemberMethod method1 = new CodeMemberMethod();
method1.Name = "TestMethod01";
method1.Attributes = (method1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public | MemberAttributes.Static;
method1.ReturnType = new CodeTypeReference(typeof(int));
method1.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(int.MaxValue)));
cd.Members.Add(method1);
CodeMemberMethod method2 = new CodeMemberMethod();
method2.Name = "TestMethod02";
method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
method2.ReturnType = new CodeTypeReference(typeof(int));
method2.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "iReturn"));
CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression(new CodeTypeReference("Foo.Foo", CodeTypeReferenceOptions.GlobalReference)), "TestMethod01"));
CodeAssignStatement cas = new CodeAssignStatement(new CodeVariableReferenceExpression("iReturn"), cmie);
method2.Statements.Add(cas);
method2.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("iReturn")));
cd.Members.Add(method2);
CodeMemberMethod method3 = new CodeMemberMethod();
method3.Name = "TestMethod03";
method3.Attributes = (method3.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
method3.ReturnType = new CodeTypeReference(typeof(int));
method3.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "iReturn"));
CodeTypeReferenceOptions ctro = CodeTypeReferenceOptions.GlobalReference;
CodeTypeReference ctr = new CodeTypeReference(typeof(Math), ctro);
cmie = new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression(ctr), "Abs"), new CodeExpression[] { new CodePrimitiveExpression(-1) });
cas = new CodeAssignStatement(new CodeVariableReferenceExpression("iReturn"), cmie);
method3.Statements.Add(cas);
method3.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("iReturn")));
cd.Members.Add(method3);
CodeMemberProperty property = new CodeMemberProperty();
property.Name = "GlobalTestProperty1";
property.Type = new CodeTypeReference(typeof(int));
property.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
property.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(fieldName1)));
property.SetStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(fieldName1), new CodeVariableReferenceExpression("value")));
cd.Members.Add(property);
CodeMemberProperty property2 = new CodeMemberProperty();
property2.Name = "GlobalTestProperty2";
property2.Type = typeRef;
property2.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
property2.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(fieldName2)));
property2.SetStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(fieldName2), new CodeVariableReferenceExpression("value")));
cd.Members.Add(property2);
AssertEqual(ns,
@"'Foo namespace
Namespace Foo
Public Class Foo
Public _verifyGlobalGeneration1 As Integer = 2147483647
Private _verifyGlobalGeneration2 As Global.System.Nullable(Of Integer) = 0
Public Property GlobalTestProperty1() As Integer
Get
Return _verifyGlobalGeneration1
End Get
Set
_verifyGlobalGeneration1 = value
End Set
End Property
Public Property GlobalTestProperty2() As Global.System.Nullable(Of Integer)
Get
Return _verifyGlobalGeneration2
End Get
Set
_verifyGlobalGeneration2 = value
End Set
End Property
Public Shared Function TestMethod01() As Integer
//.........这里部分代码省略.........
示例15: GetTypeOutput
public virtual string GetTypeOutput(CodeTypeReference type)
{
ICodeGenerator cg = CreateGenerator();
if (cg == null)
throw GetNotImplemented();
return cg.GetTypeOutput(type);
}