开发者

Invoke default parameterless valuetype constructor via Reflection.Emit

开发者 https://www.devze.com 2023-03-09 05:25 出处:网络
Is this at all possible, given Type.GetConstructors() will not return such a constructor? I have looked at this question but it still does not solve the problem.

Is this at all possible, given Type.GetConstructors() will not return such a constructor?

I have looked at this question but it still does not solve the problem.

Example:

How to emit default(Nullable<int>) or default(Guid)?

PS: I am not interested in workarounds. I can guess a few, but it would b开发者_JS百科e impossible for me to integrate them directly into my compiler without substantial modifications.


Grrr. Found my answer.

You use OpCodes.Initobj :)

http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.initobj(VS.100).aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消