- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有几个月前编写的这个框架,它生成一个类来调用此性能服务。框架的消费者使用方法创建一个接口(interface),使用属性进行注释,并调用一个工厂方法来创建他们可以用来调用此性能服务的接口(interface)的实现。该服务只支持字符串和长两种数据。我将反射发射与可收集程序集结合使用来生成实现该接口(interface)的类。
一切都运行良好,但今天有人告诉我,当他们试图传入一个将被转换为字符串的枚举时,他们得到了一个 AV。在代码中,检查类型是否为值类型,如果是,则推送地址(ldarga 或 ldflda,具体取决于消费者创建的接口(interface)),然后调用 ToString。所以我创建了一个小调试应用程序,我看到 C# 编译器将封装一个枚举,然后在封装的枚举上调用 ToString。
所以我有点困惑。我处理值类型的方式不正确吗? C# 编译器为枚举上的 toString 生成的 IL 是正确的方法吗?还有其他像这样的特殊情况吗
更新答案:所以看起来我需要查看值类型是否实现了 tostring 以及它是否不装箱。对于值类型,我想这适用于对象方法、tostring、gethashcode、equals。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;
using System.Reflection;
namespace ConsoleApplication15
{
public struct H
{
}
class Program
{
static void Main(string[] args)
{
//Test<AttributeTargets>(AttributeTargets.ReturnValue); //-- fails
//Test<int>(10); //-- works
// TestBox<AttributeTargets>(AttributeTargets.ReturnValue); //-- works
//Test<H>(new H()); // fails
TestCorrect<H>(new H()); // works
TestCorrect<int>(10); // works
Console.ReadLine();
}
private static void TestCorrect<T>(T t)
where T : struct
{
MethodInfo method = typeof(T).GetMethod(
"ToString",
BindingFlags.Public | BindingFlags.Instance,
null,
Type.EmptyTypes,
null);
var m = new DynamicMethod("x", typeof(string), new[] { typeof(T) });
var i = m.GetILGenerator();
if (method.DeclaringType == typeof(T))
{
i.Emit(OpCodes.Ldarga, 0);
i.Emit(OpCodes.Call, method);
}
else
{
i.Emit(OpCodes.Ldarg_0);
i.Emit(OpCodes.Box, typeof(T));
i.Emit(OpCodes.Callvirt, method);
}
i.Emit(OpCodes.Ret);
string result = (m.CreateDelegate(typeof(Func<T, string>)) as Func<T, string>)(t);
Console.WriteLine(result);
}
private static void Test<T>(T t)
where T : struct
{
MethodInfo method = typeof(T).GetMethod(
"ToString",
BindingFlags.Public | BindingFlags.Instance,
null,
Type.EmptyTypes,
null);
var m = new DynamicMethod("x", typeof(string), new[] { typeof(T) });
var i = m.GetILGenerator();
i.Emit(OpCodes.Ldarga, 0);
i.Emit(OpCodes.Call, method);
i.Emit(OpCodes.Ret);
string result = (m.CreateDelegate(typeof(Func<T, string>)) as Func<T, string>)(t);
Console.WriteLine(result);
}
private static void TestBox<T>(T t)
where T : struct
{
// this is how the C# compiler call to string on enum.
MethodInfo method = typeof(T).GetMethod(
"ToString",
BindingFlags.Public | BindingFlags.Instance,
null,
Type.EmptyTypes,
null);
var m = new DynamicMethod("x", typeof(string), new[] { typeof(T) });
var i = m.GetILGenerator();
i.Emit(OpCodes.Ldarg_0);
i.Emit(OpCodes.Box, typeof(T));
i.Emit(OpCodes.Callvirt, method);
i.Emit(OpCodes.Ret);
string result = (m.CreateDelegate(typeof(Func<T, string>)) as Func<T, string>)(t);
Console.WriteLine(result);
}
}
}
最佳答案
枚举类型不会覆盖其ToString()
方法,因此对于任何枚举类型e
,e.ToString()
解析到 Enum.ToString
。此方法是在引用类型上定义的(Enum
是引用类型),因此要调用此方法,隐式 this
参数需要是一个装箱值。
大多数其他值类型,例如 int
,确实直接在值类型本身上提供了一个重写的 ToString
方法。
来自规范:
I.12.1.6.2.4 Calling methods
Static methods on value types are handled no differently from static methods on an ordinary class: use a
call
instruction with a metadata token specifying the value type as the class of the method. Non-static methods (i.e., instance and virtual methods) are supported on value types, but they are given special treatment. A non-static method on a reference type (rather than a value type) expects a this pointer that is an instance of that class. This makes sense for reference types, since they have identity and the this pointer represents that identity. Value types, however, have identity only when boxed. To address this issue, the this pointer on a non-static method of a value type is a byref parameter of the value type rather than an ordinary by-value parameter.A non-static method on a value type can be called in the following ways:
For unboxed instances of a value type, the exact type is known statically. The
call
instruction can be used to invoke the function, passing as the first parameter (the this pointer) the address of the instance. The metadata token used with thecall
instruction shall specify the value type itself as the class of the method.Given a boxed instance of a value type, there are three cases to consider:
Instance or virtual methods introduced on the value type itself: unbox the instance and call the method directly using the value type as the class of the method.
Virtual methods inherited from a base class: use the
callvirt
instruction and specify the method on theSystem.Object
,System.ValueType
orSystem.Enum
class as appropriate.Virtual methods on interfaces implemented by the value type: use the
callvirt
instruction and specify the method on the interface type.
关于c# - 为什么 enum.ToString box/callvirt,而不是推送地址和调用?还有其他特殊情况吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22711443/
正在阅读 Underscore.js 以了解它的 is[String|Number|...] 方法是如何工作的,现在我很困惑。下划线: toString.call(obj) == ['object '
scala> Array(1, 2, 3).toString res1: String = [I@11cf437c scala> List(1, 2, 3).toString res2: String
我在将字符串从 stringbuilder 转换为字符串时遇到问题。问题类似于 this issue但略有不同: 这是我的简化代码: StringBuilder sb = new StringBuil
我正在尝试将从正在构建的搜索功能中名为 Part 的模型返回的 int id 转换为字符串,以便简化搜索。 这是我目前使用的 if 语句: if(part.getId().toString().ind
我需要从所选内容中提取文本并将其发送到 TTS 服务。 TTS 服务将返回一个流 URL 和每个单词的一组索引,指示它们的开始和结束位置(时间和文本)。 当用户播放流时,我想在读出每个单词时突出显示它
我想知道人们在 Java 的 toString() 方法中放入了什么。 我一直在向一些新类添加一些内容,并且想知道它是否应该包含类名。 在类ClassConfig中,我无法决定是否应该拥有 @Over
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 8 年前。 下面是我的主要方法,其中比较两个对象引用。覆盖toString()方法
我的问题是,JAVA中没有提供toString()方法的类是否可以打印出特定信息? 问题在于:我们为我们的应用程序提供了一个记录器(使用aspectJ),它打印出给出的特定参数。例如: public
基本上这就是我想要实现的目标。 classname@address(?)[original toString()], object's name, object's age @Override pub
据我所知,Scala 中的中缀运算符的使用应该等同于方法的调用。所以: scala> "a" + 3.toString res0: java.lang.String = a3 是相同的: scala>
这个问题已经有答案了: Why can't I access a property of an integer with a single dot? (5 个回答) 已关闭 7 年前。 functio
我正在进行测试,并且给出了很多单元(隐藏)测试,但是我的一段代码遇到了这个错误。大家能帮帮我吗? getString(comment) { const authorName = comment.get
return toString.call(obj) 和 return obj.toString() 有什么区别? 我通常会找到具有这些不同风格的代码 最佳答案 toString.call(obj) 返
例如,我必须在每个数字到字符串的转换中使用 .ToString(CultureInfo.CurrentCulture)。我能否以某种方式重写 .ToString(),这样我就不会在字符串转换中显式地收
var d = []; console.log(typeof d); // weird! console.log(d.toString()); //Prints nothing since there
当对象字面量调用toString()方法如{}.toString()会导致语法错误,但是当数组字面量调用toString()没关系。当我将对象文字分配给一个变量时,当它调用 toString() 方法
我在打印特殊数组时遇到问题: 我使用 System.out.println(Arrays.toString()); 打印多个对象的数组但现在数组中充满了对象,这些对象具有 char 值,我想打印分配给
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
> ~0..toString(2) -1 > ~1..toString(2) -2 > ~2..toString(2) -11 > ~3..toString(2) -12 > (~1).toStrin
这是我的问题,我的机器使用法语文化,因此默认情况下它以法语方式解析 (3,141592)。 如果机器文化不是美国,这里是重现我的问题的代码: float number = 4103.26808
我是一名优秀的程序员,十分优秀!