首页 \ 问答 \ 使用格式获取属性ToString(Get property ToString with formatting)

使用格式获取属性ToString(Get property ToString with formatting)

使用反射我可以在属性上执行ToString 。 但有没有办法在这样做时提供格式?

public static object GetCustomValue(object src, string propName)
{
    return src.GetType().GetMethod(propName).GetValue(src, null);
}

调用这样的函数工作正常

GetCustomValue(obj, "ToString")

但我想称之为

GetCustomValue(obj, "ToString(\"MMM\")")

在我的函数中调用GetMethod时,是否可以为ToString添加格式?


Using reflection I can do ToString on a property. But is there a way to supply a formatting while doing this?

public static object GetCustomValue(object src, string propName)
{
    return src.GetType().GetMethod(propName).GetValue(src, null);
}

Calling the function like this works fine

GetCustomValue(obj, "ToString")

But I would like to call it with

GetCustomValue(obj, "ToString(\"MMM\")")

Is it possible to add formatting to the ToString when calling the GetMethod in my function?


原文:https://stackoverflow.com/questions/31937128
更新时间:2023-08-23 22:08

最满意答案

在装配级别没有“未定义的内存”这样的东西。 只要行为符合预期,gcc就可以自由发出以其认为合适的方式访问堆栈的代码。

我猜测为什么会发生这种情况,这是一个叶子函数,调整堆栈指针是徒劳的。 您可以尝试通过检查程序集中的任何call指令来验证这一点。 (您也可以检查C源,但内联可能会使其不那么可靠。)

某些平台的ABI明确允许这种欺骗,包括x86-64。 从AMD64 ABI文档

超出%rsp指向的位置的128字节区域被认为是保留的,不应被信号或中断处理程序修改。 因此,函数可以将此区域用于函数调用不需要的临时数据。 特别是,叶子函数可以将这个区域用于它们的整个堆栈帧,而不是调整序言和尾声中的堆栈指针。 这个区域被称为红区。

这篇博文可能会对这个主题进行有趣的阅读。


There's no such thing as "undefined memory" at the assembly level. gcc is free to emit code that accesses the stack in whatever way it sees fit, so long as the behaviour is as expected.

My guess as to why this is happening is that this is a leaf function for which adjusting the stack pointer is fruitless. You could try to verify that by inspecting the assembly for any call instructions. (You could also inspect the C source, but inlining may make that a bit less reliable.)

This kind of trickery is explicitly allowed by the ABI of certain platforms, including x86-64. From the AMD64 ABI documentation:

The 128-byte area beyond the location pointed to by %rsp is considered to be reserved and shall not be modified by signal or interrupt handlers. Therefore, functions may use this area for temporary data that is not needed across function calls. In particular, leaf functions may use this area for their entire stack frame, rather than adjusting the stack pointer in the prologue and epilogue. This area is known as the red zone.

This blog post might make for interesting reading on the subject.

相关问答

更多
  • 没关系,我想我已经找到了它(可能在Martins所做的大致同一时间)。 看起来glVertexAttribPointer在调用glDrawElements之前不会复制数据。 由于我的glDrawElements不在该函数中,因此堆栈内存被释放(但由于某种原因, texices2仍然有效)。 我已经重写了一些代码, texices在分配该类时,将texices分配一次,并在应用程序deallocs时反复重复使用并释放。 Nevermind, I think I've found it (probably at ...
  • 是的,您可以访问堆栈中的项目,因为堆栈只是内存。 但是,如果您处于16位模式,则无法使用DX ,您需要BX , SI , DI或BP 。 例如: mov bx, sp mov bl, ss:[bx+10] 请注意,除BP外,您需要ss段覆盖。 在32位模式下,您可以直接使用ESP ,例如: mov dl, [esp+10] PS:对于第一个程序来说,这是一个相当奇怪的项目;) Yes, you can access items on the stack because stack is just mem ...
  • 堆分配是比汇编语言更高级(C,java等)的概念,你在808x汇编中没有这样的东西。 你没有“分配”内存。 另一方面,堆栈是808x的原生概念,以及无数的微处理器(和虚拟机),每次使用“CALL”时都会使用它(PC被压入堆栈)。 RET时,返回的PC地址从堆栈中弹出。 您也可以将寄存器/数据推送到堆栈中。 Heap allocation is a higher-level (C, java, etc) concept than assembly language, you don't have such t ...
  • 问题在于 stack[to] = push(stack[i], k); 应该 stack[to] = push(stack[to], k); 感谢Joe Z. Problem was in stack[to] = push(stack[i], k); Should be stack[to] = push(stack[to], k); Thanks to Joe Z.
  • 问题是你不想重新分配堆栈的顶部。 相反,您希望分配一个新的值数组,该数组足够大以容纳新值。 此外,由于您需要重新分配数组,因此values应该是指针。 但是我们忘记了这一切呢。 如果我们使用c ++,那么让我们使用c ++为我们提供的东西,让我们的生活更轻松。 完成之后,如果你真的觉得有需要,那就尝试打开一些东西。 我指的其中一件事是你使用calloc 。 使用calloc是一个坏主意,特别是在使用模板时。 问题是,由于calloc没有类型信息,因此它不会像调用构造函数那样执行基本操作。 构造函数在OOP中 ...
  • 在装配级别没有“未定义的内存”这样的东西。 只要行为符合预期,gcc就可以自由发出以其认为合适的方式访问堆栈的代码。 我猜测为什么会发生这种情况,这是一个叶子函数,调整堆栈指针是徒劳的。 您可以尝试通过检查程序集中的任何call指令来验证这一点。 (您也可以检查C源,但内联可能会使其不那么可靠。) 某些平台的ABI明确允许这种欺骗,包括x86-64。 从AMD64 ABI文档 : 超出%rsp指向的位置的128字节区域被认为是保留的,不应被信号或中断处理程序修改。 因此,函数可以将此区域用于函数调用不需要的 ...
  • Pop不会调用析构函数。 你可以使用类似的东西 T *topObject = my_stack->top(); my_stack->pop(); delete topObject; Pop does not call the destructor. You can use something like T *topObject = my_stack->top(); my_stack->pop(); delete topObject;
  • 该堆栈实际上是一个LIFO堆栈。 它是在程序运行时由程序本身创建的。 控制堆栈的代码是在编译时创建的。 更远的阅读: 硬件堆栈 调用堆栈 X86寻址模式 The stack is in fact a LIFO stack. It's created by the program itself while the program is running. The code that controls the stack is created at compilation time. Farther readin ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。