小编Can*_*ax_的帖子

如何摆脱虚拟表?密封类

据我所知,让班级sealed摆脱VTable中的查找或我错了吗?如果我创建一个类sealed,这是否意味着类层次结构中的所有虚方法也都标记了sealed

例如:

public class A {
    protected virtual void M() { ........ }
    protected virtual void O() { ........ }
}

public sealed class B : A {
    // I guess I can make this private for sealed class
    private override void M() { ........ }
    // Is this method automatically sealed? In the meaning that it doesn't have to look in VTable and can be called directly?

    // Also what about O() can …
Run Code Online (Sandbox Code Playgroud)

c# oop inheritance sealed vtable

9
推荐指数
1
解决办法
480
查看次数

C# GetMethod 与 GetRuntimeMethod

我正在使用GetRuntimeMethod一种方法来获取类型之外的方法。一切正常,但我注意到有一个GetMethod. 两者有什么区别?

this.target.GetType().GetRuntimeMethod(
    "MethodName",
    new System.Type[0]
)?.Invoke(targetObject, null);
Run Code Online (Sandbox Code Playgroud)

从名称中我只能猜测它GetRuntimeMethod在程序运行时起作用。并GetMethod在程序编译期间起作用?

使用其中一种替代另一种有什么优点吗?(这最后一个问题当然可以从答案中推断出两者之间有什么区别)

c# reflection

2
推荐指数
1
解决办法
2336
查看次数

包含字符串的结构与包含字符串的类的 C# 性能

请查看这两个小例子:

public struct Struct
{
    public readonly string StringValue;
    public readonly int IntValue;

    public Struct(string stringValue)
    {
        this.StringValue = stringValue;
        this.IntValue = this.StringValue.GetHashCode(); // Just ignore this assignment, please :).
    }
}

public class Class
{
    public readonly string StringValue;
    public readonly int IntValue;

    public Class(string stringValue)
    {
        this.StringValue = stringValue;
        this.IntValue = this.StringValue.GetHashCode(); // Just ignore this assignment, please :).
    }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,如果我将 struct 作为参数传递,它将制作它的副本someObject.Method(this.cachedStruct);- 这反过来将制作 a 的副本,StringValue因为strings 是不可变的,它将在内存中为该字符串分配数组,这是非常昂贵的操作 [请原谅我,如果我犯了一个错误,请在这种情况下纠正我]。

因此,在这种情况下,我认为使用 的实例Class …

c# string heap performance cil

2
推荐指数
1
解决办法
229
查看次数

标签 统计

c# ×3

cil ×1

heap ×1

inheritance ×1

oop ×1

performance ×1

reflection ×1

sealed ×1

string ×1

vtable ×1