给定“where T:new()”,“new T()”是否成立?在内部使用 Activator.CreateInstance 吗?

发布于 2024-11-24 07:29:37 字数 212 浏览 6 评论 0原文

如果我有类型参数约束 new()

void Foo<T>() where T : new()
{
    var t = new T();
}

new T() 是否真的会在内部使用 Activator.CreateInstance 方法(即反射)?

If I have a type parameter constraint new():

void Foo<T>() where T : new()
{
    var t = new T();
}

Is it true that new T() will internally use the Activator.CreateInstance method (i.e. reflection)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭⌒浅淡时光〆 2024-12-01 07:29:37

是的,这是真的。 编辑 2: 这里很好地解释了如何以及为什么。

http://www.simple-talk .com/community/blogs/simonc/archive/2010/11/17/95700.aspx

为了验证我编译了以下方法:

public static T Create<T>() where T: new() {
    return new T();
}

这是生成的使用 .NET 3.5 SP1 中的 C# 编译器编译时的 IL:

.method public hidebysig static !!T Create<.ctor T>() cil managed
{
    .maxstack 2
    .locals init (
        [0] !!T local,
        [1] !!T local2)
    L_0000: ldloca.s local
    L_0002: initobj !!T
    L_0008: ldloc.0 
    L_0009: box !!T
    L_000e: brfalse.s L_001a
    L_0010: ldloca.s local2
    L_0012: initobj !!T
    L_0018: ldloc.1 
    L_0019: ret 
    L_001a: call !!0 [mscorlib]System.Activator::CreateInstance<!!T>()
    L_001f: ret 
}

编辑: C# 4 编译器创建略有不同但相似的代码:

.method public hidebysig static !!T Create<.ctor T>() cil managed
{
    .maxstack 2
    .locals init (
        [0] !!T CS$1$0000,
        [1] !!T CS$0$0001)
    L_0000: nop 
    L_0001: ldloca.s CS$0$0001
    L_0003: initobj !!T
    L_0009: ldloc.1 
    L_000a: box !!T
    L_000f: brfalse.s L_001c
    L_0011: ldloca.s CS$0$0001
    L_0013: initobj !!T
    L_0019: ldloc.1 
    L_001a: br.s L_0021
    L_001c: call !!0 [mscorlib]System.Activator::CreateInstance<!!T>()
    L_0021: stloc.0 
    L_0022: br.s L_0024
    L_0024: ldloc.0 
    L_0025: ret 
}

对于值类型,它不使用激活器,而是使用激活器仅返回 default(T) 值,否则它会调用 Activator.CreateInstance 方法。

Yes, this is true. Edit 2: Here's a good explanation of the how and why.

http://www.simple-talk.com/community/blogs/simonc/archive/2010/11/17/95700.aspx

For verification I compiled the following method:

public static T Create<T>() where T: new() {
    return new T();
}

And this is the generated IL when compiled with the C# compiler in .NET 3.5 SP1:

.method public hidebysig static !!T Create<.ctor T>() cil managed
{
    .maxstack 2
    .locals init (
        [0] !!T local,
        [1] !!T local2)
    L_0000: ldloca.s local
    L_0002: initobj !!T
    L_0008: ldloc.0 
    L_0009: box !!T
    L_000e: brfalse.s L_001a
    L_0010: ldloca.s local2
    L_0012: initobj !!T
    L_0018: ldloc.1 
    L_0019: ret 
    L_001a: call !!0 [mscorlib]System.Activator::CreateInstance<!!T>()
    L_001f: ret 
}

Edit: The C# 4 compiler creates slightly different, but similar, code:

.method public hidebysig static !!T Create<.ctor T>() cil managed
{
    .maxstack 2
    .locals init (
        [0] !!T CS$1$0000,
        [1] !!T CS$0$0001)
    L_0000: nop 
    L_0001: ldloca.s CS$0$0001
    L_0003: initobj !!T
    L_0009: ldloc.1 
    L_000a: box !!T
    L_000f: brfalse.s L_001c
    L_0011: ldloca.s CS$0$0001
    L_0013: initobj !!T
    L_0019: ldloc.1 
    L_001a: br.s L_0021
    L_001c: call !!0 [mscorlib]System.Activator::CreateInstance<!!T>()
    L_0021: stloc.0 
    L_0022: br.s L_0024
    L_0024: ldloc.0 
    L_0025: ret 
}

In the case of a value type it doesn't use the activator but just returns the default(T) value, otherwise it invokes the Activator.CreateInstance method.

南街女流氓 2024-12-01 07:29:37

是的。它适用于引用类型。

在以下发布编译的代码上使用 ILSpy:

    public static void DoWork<T>() where T: new()
    {
        T t = new T();
        Console.WriteLine(t.ToString());
    }

Yielded

.method public hidebysig 
    instance void DoWork<.ctor T> () cil managed 
{
    // Method begins at RVA 0x2064
    // Code size 52 (0x34)
    .maxstack 2
    .locals init (
        [0] !!T t,
        [1] !!T CS$0$0000,
        [2] !!T CS$0$0001
    )

    IL_0000: ldloca.s CS$0$0000
    IL_0002: initobj !!T
    IL_0008: ldloc.1
    IL_0009: box !!T
    IL_000e: brfalse.s IL_001b

    IL_0010: ldloca.s CS$0$0001
    IL_0012: initobj !!T
    IL_0018: ldloc.2
    IL_0019: br.s IL_0020

    IL_001b: call !!0 [mscorlib]System.Activator::CreateInstance<!!T>()

    IL_0020: stloc.0
    IL_0021: ldloca.s t
    IL_0023: constrained. !!T
    IL_0029: callvirt instance string [mscorlib]System.Object::ToString()
    IL_002e: call void [mscorlib]System.Console::WriteLine(string)
    IL_0033: ret
} // end of method Program::DoWork

或在 C# 中:

public void DoWork<T>() where T : new()
{
    T t = (default(T) == null) ? Activator.CreateInstance<T>() : default(T);
    Console.WriteLine(t.ToString());
}

JIT 将为传入的每个不同值类型参数创建不同的编译指令,但将对引用类型使用相同的指令 - 因此 Activator.CreateInstance()

Yes. It does for reference types.

Using ILSpy on the following release-compiled code:

    public static void DoWork<T>() where T: new()
    {
        T t = new T();
        Console.WriteLine(t.ToString());
    }

Yielded

.method public hidebysig 
    instance void DoWork<.ctor T> () cil managed 
{
    // Method begins at RVA 0x2064
    // Code size 52 (0x34)
    .maxstack 2
    .locals init (
        [0] !!T t,
        [1] !!T CS$0$0000,
        [2] !!T CS$0$0001
    )

    IL_0000: ldloca.s CS$0$0000
    IL_0002: initobj !!T
    IL_0008: ldloc.1
    IL_0009: box !!T
    IL_000e: brfalse.s IL_001b

    IL_0010: ldloca.s CS$0$0001
    IL_0012: initobj !!T
    IL_0018: ldloc.2
    IL_0019: br.s IL_0020

    IL_001b: call !!0 [mscorlib]System.Activator::CreateInstance<!!T>()

    IL_0020: stloc.0
    IL_0021: ldloca.s t
    IL_0023: constrained. !!T
    IL_0029: callvirt instance string [mscorlib]System.Object::ToString()
    IL_002e: call void [mscorlib]System.Console::WriteLine(string)
    IL_0033: ret
} // end of method Program::DoWork

Or in C#:

public void DoWork<T>() where T : new()
{
    T t = (default(T) == null) ? Activator.CreateInstance<T>() : default(T);
    Console.WriteLine(t.ToString());
}

JIT will create different compiled instructions for each different value type parameter passed in, but will use the same instructions for reference types -- hence the Activator.CreateInstance()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文