CodeDefaultValueExpression 类

定义

表示对默认值的引用。Represents a reference to a default value.

public ref class CodeDefaultValueExpression : System::CodeDom::CodeExpression
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeDefaultValueExpression : System.CodeDom.CodeExpression
type CodeDefaultValueExpression = class
    inherit CodeExpression
Public Class CodeDefaultValueExpression
Inherits CodeExpression
继承
CodeDefaultValueExpression
属性

示例

下面的代码示例演示CodeDefaultValueExpression如何使用来创建 decimal 和 integer 参数的默认值。The following code example shows the use of the CodeDefaultValueExpression to create default values for decimal and integer parameters. 此示例是为CodeTypeParameter类提供的更大示例的一部分。This example is part of a larger example provided for the CodeTypeParameter class.

printMethod.Statements.Add(ConsoleWriteLineStatement(
    new CodeDefaultValueExpression(new CodeTypeReference("T"))));
printMethod.Statements.Add(ConsoleWriteLineStatement(
    new CodeDefaultValueExpression(new CodeTypeReference("S"))));
printMethod.Statements.Add(ConsoleWriteLineStatement _
    (New CodeDefaultValueExpression(New CodeTypeReference("T"))))
printMethod.Statements.Add(ConsoleWriteLineStatement _
    (New CodeDefaultValueExpression(New CodeTypeReference("S"))))

注解

CodeDefaultValueExpression可用于表示对默认值的引用。A CodeDefaultValueExpression can be used to represent a reference to a default value.

Type属性指定对值类型的引用。The Type property specifies the reference to the value type. CodeDefaultValueExpression类在生成基于泛型的代码时使用。The CodeDefaultValueExpression class is used in the generation of generics-based code. 有关泛型的详细信息, 请参阅.NET Framework 类库中的泛型For more information on generics, see Generics in the .NET Framework Class Library. 此部分中提供了下面的代码步骤, 以进一步说明如何使用CodeDefaultValueExpression类向代码图形添加新的默认值。The following code steps are provided in this section to further describe the use of the CodeDefaultValueExpression class to add a new default value to a code graph.

第1部分中的代码是为CodeTypeParameter类提供的更大示例的一部分。The code in part 1 is part of a larger example provided for the CodeTypeParameter class. 此代码在通过C#代码生成器运行时, 会生成在第C# 2 部分中显示的代码。This code, when run through the C# code generator, results in the C# code that appears in part 2. 如果在第3部分的语句中调用此代码, 则结果为第4部分中显示的输出。When this code is called in the statement in part 3, the result is the output shown in part 4.

// Part 1: Code to create a generic Print 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"))));  
// Part 2: Code generated by code in part 1.  
public virtual void Print<S, T>()  
            where S : new()  
         {  
            Console.WriteLine(default(T));  
            Console.WriteLine(default(S));  
        }  
// Part 3: Call to the generated method.  
dict.Print<System.Decimal, int>();  
// Part 4: Output of the generated method.  
0  
0  
  

构造函数

CodeDefaultValueExpression()

初始化 CodeDefaultValueExpression 类的新实例。Initializes a new instance of the CodeDefaultValueExpression class.

CodeDefaultValueExpression(CodeTypeReference)

使用指定的代码类型引用初始化 CodeDefaultValueExpression 类的新实例。Initializes a new instance of the CodeDefaultValueExpression class using the specified code type reference.

属性

Type

获取或设置默认值的数据类型引用。Gets or sets the data type reference for a default value.

UserData

获取当前对象的用户可定义数据。Gets the user-definable data for the current object.

(继承自 CodeObject)

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于