好的方法来检查这是写小文件,编译它们并检查与IL间谍他们在中间语言中做什么。如果我这样做,我遇到这种情况。这是C#代码。
using System;
namespace test
{
public class NameIdObject
{
public string Name { get; set; }
public int Id { get; set; }
}
public static class MyCache
{
public static NameIdObject MyCacheObject = new NameIdObject();
}
public static class Program
{
public static void Main()
{
MyCache.MyCacheObject.Name = "foo";
MyCache.MyCacheObject.Id = 123;
}
}
}
案例2
using System;
namespace test
{
public class NameIdObject
{
public string Name { get; set; }
public int Id { get; set; }
}
public static class MyCache
{
public static NameIdObject MyCacheObject;
}
public static class Program
{
public static void Main()
{
MyCache.MyCacheObject = new NameIdObject { Id = 123, Name = "foo" };
}
}
}
当您编译低谷开发命令行。有关详细信息,请参见https://msdn.microsoft.com/en-us/library/78f4aasd.aspx。您可以查看与IL间谍中间语言
下面是结果:
案例1
.namespace test
{
.class public auto ansi abstract sealed beforefieldinit test.MyCache
extends [mscorlib]System.Object
{
// Fields
.field public static class test.NameIdObject MyCacheObject
// Methods
.method private hidebysig specialname rtspecialname static
void .cctor() cil managed
{
// Method begins at RVA 0x207b
// Code size 11 (0xb)
.maxstack 8
IL_0000: newobj instance void test.NameIdObject::.ctor()
IL_0005: stsfld class test.NameIdObject test.MyCache::MyCacheObject
IL_000a: ret
} // end of method MyCache::.cctor
} // end of class test.MyCache
.class public auto ansi beforefieldinit test.NameIdObject
extends [mscorlib]System.Object
{
// Fields
.field private string '<Name>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
.custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = (
01 00 00 00 00 00 00 00
)
.field private int32 '<Id>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
.custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = (
01 00 00 00 00 00 00 00
)
// Methods
.method public hidebysig specialname
instance string get_Name() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2050
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld string test.NameIdObject::'<Name>k__BackingField'
IL_0006: ret
} // end of method NameIdObject::get_Name
.method public hidebysig specialname
instance void set_Name (
string 'value'
) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2058
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld string test.NameIdObject::'<Name>k__BackingField'
IL_0007: ret
} // end of method NameIdObject::set_Name
.method public hidebysig specialname
instance int32 get_Id() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2061
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld int32 test.NameIdObject::'<Id>k__BackingField'
IL_0006: ret
} // end of method NameIdObject::get_Id
.method public hidebysig specialname
instance void set_Id (
int32 'value'
) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2069
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld int32 test.NameIdObject::'<Id>k__BackingField'
IL_0007: ret
} // end of method NameIdObject::set_Id
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Method begins at RVA 0x2072
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method NameIdObject::.ctor
// Properties
.property instance string Name()
{
.get instance string test.NameIdObject::get_Name()
.set instance void test.NameIdObject::set_Name(string)
}
.property instance int32 Id()
{
.get instance int32 test.NameIdObject::get_Id()
.set instance void test.NameIdObject::set_Id(int32)
}
} // end of class test.NameIdObject
.class public auto ansi abstract sealed beforefieldinit test.Programm
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig static
void Main() cil managed
{
// Method begins at RVA 0x2087
// Code size 31 (0x1f)
.maxstack 8
.entrypoint
IL_0000: nop
IL_0001: ldsfld class test.NameIdObject test.MyCache::MyCacheObject
IL_0006: ldstr "foo"
IL_000b: callvirt instance void test.NameIdObject::set_Name(string)
IL_0010: nop
IL_0011: ldsfld class test.NameIdObject test.MyCache::MyCacheObject
IL_0016: ldc.i4.s 123
IL_0018: callvirt instance void test.NameIdObject::set_Id(int32)
IL_001d: nop
IL_001e: ret
} // end of method Programm::Main
} // end of class test.Programm
}
案例现在2
.namespace test
{
.class public auto ansi abstract sealed beforefieldinit test.MyCache
extends [mscorlib]System.Object
{
// Fields
.field public static class test.NameIdObject MyCacheObject
} // end of class test.MyCache
.class public auto ansi beforefieldinit test.NameIdObject
extends [mscorlib]System.Object
{
// Fields
.field private string '<Name>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
.custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = (
01 00 00 00 00 00 00 00
)
.field private int32 '<Id>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
.custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = (
01 00 00 00 00 00 00 00
)
// Methods
.method public hidebysig specialname
instance string get_Name() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2050
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld string test.NameIdObject::'<Name>k__BackingField'
IL_0006: ret
} // end of method NameIdObject::get_Name
.method public hidebysig specialname
instance void set_Name (
string 'value'
) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2058
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld string test.NameIdObject::'<Name>k__BackingField'
IL_0007: ret
} // end of method NameIdObject::set_Name
.method public hidebysig specialname
instance int32 get_Id() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2061
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld int32 test.NameIdObject::'<Id>k__BackingField'
IL_0006: ret
} // end of method NameIdObject::get_Id
.method public hidebysig specialname
instance void set_Id (
int32 'value'
) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2069
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld int32 test.NameIdObject::'<Id>k__BackingField'
IL_0007: ret
} // end of method NameIdObject::set_Id
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Method begins at RVA 0x2072
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method NameIdObject::.ctor
// Properties
.property instance string Name()
{
.get instance string test.NameIdObject::get_Name()
.set instance void test.NameIdObject::set_Name(string)
}
.property instance int32 Id()
{
.get instance int32 test.NameIdObject::get_Id()
.set instance void test.NameIdObject::set_Id(int32)
}
} // end of class test.NameIdObject
.class public auto ansi abstract sealed beforefieldinit test.Program
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig static
void Main() cil managed
{
// Method begins at RVA 0x207b
// Code size 33 (0x21)
.maxstack 8
.entrypoint
IL_0000: nop
IL_0001: newobj instance void test.NameIdObject::.ctor()
IL_0006: dup
IL_0007: ldc.i4.s 123
IL_0009: callvirt instance void test.NameIdObject::set_Id(int32)
IL_000e: nop
IL_000f: dup
IL_0010: ldstr "foo"
IL_0015: callvirt instance void test.NameIdObject::set_Name(string)
IL_001a: nop
IL_001b: stsfld class test.NameIdObject test.MyCache::MyCacheObject
IL_0020: ret
} // end of method Program::Main
} // end of class test.Program
}
这个信息,你面包车分析系统在你的代码中做了什么。您可以在案例1中看到NameIdObject的实例是在MyCache对象的构造函数中创建的。而在情况2中,在程序的主函数中创建了NameIdObject的实例。
因此,创建对象实例的位置以及实例创建的时间不同。我希望这有助于确定哪种情况对您的程序最有效。
这两种情况在对象构造方式上都是相似的。“MyCache.MyCacheObject”总是被初始化。 –
关于记忆力,他们几乎是平等的。情况2使用延迟实例化,这会延迟内存分配。这更多的是设计决定。编辑:这假定,在情况2中,你不直接在'MyCache'中初始化对象。 –