CodeTypeParameter
メンバ
コンストラクタ
- public CodeTypeParameter();
-
- 概要
- System.CodeDom.CodeTypeParameter クラスの新しいインスタンスを初期化します。
- public CodeTypeParameter( string name );
-
- 概要
- 指定した型パラメーター名を使用して CodeTypeParameter クラスの新しいインスタンスを初期化します。
- パラメーター
-
- name
- 型パラメーターの名前。
プロパティ
- public string Name{ get; set; }
-
- 概要
- 型パラメーターの名前を取得または設定します。
- 戻り値
- 型パラメーターの名前。 既定値は、空の文字列 ("") です。
-
public CodeTypeReferenceCollection Constraints{ get; }
【 CodeTypeReference 】 -
- 概要
- 型パラメーターの制約を取得します。
- 戻り値
- A System.CodeDom.CodeTypeReferenceCollection オブジェクト、型パラメーターの制約が含まれています。
-
public CodeAttributeDeclarationCollection CustomAttributes{ get; }
【 CodeAttributeDeclaration 】 -
- 概要
- 型パラメーターのカスタム属性を取得します。
- 戻り値
- A System.CodeDom.CodeAttributeDeclarationCollection 型パラメーターのカスタム属性を示します。 既定値は、nullです。
- public bool HasConstructorConstraint{ get; set; }
-
- 概要
- 型パラメーターにコンストラクターの制約があるかどうかを表す値を取得または設定します。
- 戻り値
- true 型パラメーターには、コンス トラクターの制約がある場合それ以外の場合、 falseです。 既定値は、false です。
- UserData
- 現在のオブジェクトのユーザー定義可能なデータを取得します。 (Inherited from CodeObject )
メソッド
- Equals(Object)
- 指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。 (Inherited from Object)
- GetHashCode()
- 既定のハッシュ関数として機能します。 (Inherited from Object)
- GetType()
- 現在のインスタンスの Type を取得します。 (Inherited from Object)
- MemberwiseClone()
- 現在の Object の簡易コピーを作成します。 (Inherited from Object)
- ToString()
- 現在のオブジェクトを表す文字列を返します。 (Inherited from Object)
例
次のコード例の使用を示しています、CodeTypeParameterジェネリック コードを含むアプリケーションを生成する CodeDOM グラフを作成するクラス。
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Collections.Generic;
namespace System.CodeDom
{
class CodeDomGenericsDemo
{
static void Main()
{
try
{
CreateGenericsCode( "cs", "Generic.cs", "GenericCS.exe" );
}
catch ( Exception e )
{
LogMessage( "Unexpected Exception:" + e.ToString() );
}
}
static void CreateGenericsCode( string providerName, string sourceFileName, string assemblyName )
{
CodeDomProvider provider = CodeDomProvider.CreateProvider( providerName );
LogMessage( "Building CodeDOM graph..." );
CodeCompileUnit cu = new CodeCompileUnit();
CreateGraph( provider, cu );
StringWriter sw = new StringWriter();
LogMessage( "Generating code..." );
provider.GenerateCodeFromCompileUnit( cu, sw, null );
string output = sw.ToString();
output = Regex.Replace( output, "Runtime Version:[^"r"n]*", "Runtime Version omitted for demo" );
LogMessage( "Dumping source..." );
LogMessage( output );
LogMessage( "Writing source to file..." );
Stream s = File.Open( sourceFileName, FileMode.Create );
StreamWriter t = new StreamWriter( s );
t.Write( output );
t.Close();
s.Close();
CompilerParameters opt =
new CompilerParameters(
new string[]
{
"System.dll",
"System.Xml.dll",
"System.Windows.Forms.dll",
"System.Data.dll",
"System.Drawing.dll"
}
);
opt.GenerateExecutable = false;
opt.TreatWarningsAsErrors = true;
opt.IncludeDebugInformation = true;
opt.GenerateInMemory = true;
CompilerResults results;
LogMessage( "Compiling with " + providerName );
results = provider.CompileAssemblyFromFile( opt, sourceFileName );
OutputResults( results );
if ( results.NativeCompilerReturnValue != 0 )
{
LogMessage( "" );
LogMessage( "Compilation failed." );
}
else
{
LogMessage( "" );
LogMessage( "Demo completed successfully." );
}
File.Delete( sourceFileName );
}
// Create a CodeDOM graph.
// CodeDOMグラフを作成しなさい。
static void CreateGraph( CodeDomProvider provider, CodeCompileUnit cu )
{
if ( !provider.Supports( GeneratorSupport.GenericTypeReference |
GeneratorSupport.GenericTypeDeclaration ) )
{
// Return if the generator does not support generics.
// もしジェネレータがジェネリック医薬品をサポートしないならば戻りなさい。
return;
}
CodeNamespace ns = new CodeNamespace( "DemoNamespace" );
ns.Imports.Add( new CodeNamespaceImport( "System" ) );
ns.Imports.Add( new CodeNamespaceImport( "System.Collections.Generic" ) );
cu.Namespaces.Add( ns );
// Declare a generic class.
// 一般的なクラスを宣言しなさい。
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 )
)
);
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 = "Print";
printMethod.TypeParameters.Add( sType );
printMethod.TypeParameters.Add( tType );
printMethod.Statements.Add(
ConsoleWriteLineStatement(
new CodeDefaultValueExpression( new CodeTypeReference( "T" ) )
)
);
printMethod.Statements.Add(
ConsoleWriteLineStatement(
new CodeDefaultValueExpression( new CodeTypeReference( "S" ) )
)
);
printMethod.Attributes = MemberAttributes.Public;
class1.Members.Add( printMethod );
CodeTypeDeclaration class2 = new CodeTypeDeclaration();
class2.Name = "Demo";
CodeEntryPointMethod methodMain = new CodeEntryPointMethod();
CodeTypeReference 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 )
)
);
methodMain.Statements.Add(
ConsoleWriteLineStatement(
new CodePropertyReferenceExpression(
new CodeVariableReferenceExpression( "dict" ),
"Count"
)
)
);
methodMain.Statements.Add(
new CodeExpressionStatement(
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeVariableReferenceExpression( "dict" ),
"Print",
new CodeTypeReference[]
{
new CodeTypeReference("System.Decimal"),
new CodeTypeReference("System.Int32"),
}
),
new CodeExpression[ 0 ]
)
)
);
string dictionaryTypeName =
typeof(
System.Collections.Generic.Dictionary>[]
).FullName;
CodeTypeReference dictionaryType = new CodeTypeReference( dictionaryTypeName );
methodMain.Statements.Add(
new CodeVariableDeclarationStatement(
dictionaryType,
"dict2",
new CodeArrayCreateExpression(
dictionaryType,
new CodeExpression[ 1 ]
{
new CodePrimitiveExpression( null )
}
)
)
);
methodMain.Statements.Add(
ConsoleWriteLineStatement(
new CodePropertyReferenceExpression(
new CodeVariableReferenceExpression( "dict2" ),
"Length"
)
)
);
class2.Members.Add( methodMain );
ns.Types.Add( class2 );
}
static CodeStatement ConsoleWriteLineStatement( CodeExpression exp )
{
return
new CodeExpressionStatement(
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression(
new CodeTypeReference( "Console" )
),
"WriteLine"
),
new CodeExpression[]
{
exp,
}
)
);
}
static CodeStatement ConsoleWriteLineStatement( string text )
{
return ConsoleWriteLineStatement( new CodePrimitiveExpression( text ) );
}
static void LogMessage( string text )
{
Console.WriteLine( text );
}
static void OutputResults( CompilerResults results )
{
LogMessage(
"NativeCompilerReturnValue=" +
results.NativeCompilerReturnValue.ToString()
);
foreach ( string s in results.Output )
{
LogMessage( s );
}
}
}
}
// This example generates the following code:
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version omitted for demo
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
//namespace DemoNamespace
//{
// using System;
// using System.Collections.Generic;
// public class MyDictionary<[System.ComponentModel.DescriptionAttribute("KeyType")] TKey,
// [System.ComponentModel.DescriptionAttribute("ValueType")] TValue> : Dictionary
// where TKey : System.IComparable, IComparable, new()
// where TValue : System.Collections.Generic.IList
// {
// public virtual void Print()
// where S : new()
// {
// Console.WriteLine(default(T));
// Console.WriteLine(default(S));
// }
// }
// public class Demo
// {
// public static void Main()
// {
// MyDictionary> dict = new MyDictionary>();
// Console.WriteLine(dict.Count);
// dict.Print();
// System.Collections.Generic.Dictionary>[] dict2 =
// new System.Collections.Generic.Dictionary>[] { null };
// Console.WriteLine(dict2.Length);
// }
// }
//}
定義
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeTypeParameter : System.CodeDom.CodeObject
クラス
#region アセンブリ System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:¥Program Files (x86)¥Reference Assemblies¥Microsoft¥Framework¥.NETFramework¥v4.6.1¥System.dll
#endregion
using System.Runtime.InteropServices;
namespace System.CodeDom
{
//
// 概要:
// ジェネリック型またはメソッドの型パラメーターを表します。
[ClassInterface( ClassInterfaceType.AutoDispatch )]
[ComVisible( true )]
public class CodeTypeParameter : CodeObject
{
//
// 概要:
// System.CodeDom.CodeTypeParameter クラスの新しいインスタンスを初期化します。
public CodeTypeParameter();
//
// 概要:
// 新しいインスタンスを初期化、 System.CodeDom.CodeTypeParameter 指定した型のパラメーターの名前を持つクラス。
//
// パラメーター:
// name:
// 型パラメーターの名前。
public CodeTypeParameter( string name );
//
// 概要:
// 取得または型パラメーターの名前を設定します。
//
// 戻り値:
// 型パラメーターの名前。 既定値は、空の文字列 ("") です。
public string Name
{
get;
set;
}
//
// 概要:
// 型パラメーターの制約を取得します。
//
// 戻り値:
// A System.CodeDom.CodeTypeReferenceCollection オブジェクト、型パラメーターの制約が含まれています。
public CodeTypeReferenceCollection Constraints
{
get;
}
//
// 概要:
// 型パラメーターのカスタム属性を取得します。
//
// 戻り値:
// A System.CodeDom.CodeAttributeDeclarationCollection 型パラメーターのカスタム属性を示します。 既定値は、null
// です。
public CodeAttributeDeclarationCollection CustomAttributes
{
get;
}
//
// 概要:
// 取得または型パラメーターがコンス トラクターの制約を持っているかどうかを示す値を設定します。
//
// 戻り値:
// true 型パラメーターには、コンス トラクターの制約がある場合それ以外の場合、 falseです。 既定値は、false です。
public bool HasConstructorConstraint
{
get;
set;
}
}
}