Skip to content

Instantly share code, notes, and snippets.

@Blokyk
Created December 29, 2023 02:06
Show Gist options
  • Save Blokyk/31874148cc01e2350bd05ffda5bc7c0d to your computer and use it in GitHub Desktop.
Save Blokyk/31874148cc01e2350bd05ffda5bc7c0d to your computer and use it in GitHub Desktop.
Notes: Delegates as custom attribute argument

Notes: Add support for delegates as attribute arguments

Original proposal

About dotnet/runtime

  • Don't open the repo in vscode with the c/c++ extension enabled, it will hoard enormous amounts of resources.

  • Be sure to build clr+libs in Debug the first time; then you can build just the clr (as long as you don't touch the libs, duh).

  • To try the changes out, use artifacts/bin/coreclr/linux.x64.Debug/corerun, though you'll probably first have to copy the contents of linux.x64.Debug/libs into linux.x64.Debug/. You should probably also set CORE_LIBRARIES to artifacts/bin/runtime/net-9.0-linux-Debug-x64.

  • Workflow guide

  • Debugging the runtime on linux

  • docs/ Index

  • Testing the runtime

Tools

  • CFF Explorer
  • mdv, the metadata visualizer
  • ilasm and ildasm from dotnet/runtime

Articles

Interesting files/places

dotnet/runtime

current tree

  • inc/caparser.h -- helper to turn the raw data from blobs into the target type (int, string, type, etc...)
    • methods like GetData also use inc/strgpooli.h(29): CPackedLen to decode some compressed data
  • inc/corhdr.h(932): CorSerializationType -- list of types that can be serialized for attributes
  • vm/customattribute.cpp(222): ParseCaValue -- used in loader to parse attr arguments
  • md/ -- heart of the metadata loading/reading
    • md/compiler/custattr.h
    • md/compiler/custattr_import.cpp
    • md/inc/cahlprinternal.h(56): CaValue -- structure holding the actual value

Problems

  • what should we actually encode (answer: probably just MD tokens)
  • how do we construct the actual delegate from what we encoded
  • delegate could end-up in object[], how do we handle boxing in that case?
  • encoding generic methods and/or methods on generic types
  • what restrictions have to be in place for this to work out? (e.g. cannot be instance lambda/method group, obviously)
  • what restrictions should be in place to make our lives easier?
  • we'll also have to change a bunch of other things/tools: compiler back-end + mid-end (for ex. GetAttributeData could now return IMethodInfo), mdv, ilasm+ildasm, System.Reflection.Metadata/Emit
  • do we even want to give a delegate??? Like a MethodInfo would allow getting instance delegates for example, it'd then be up to the user to actually instantiate them
  • how do we reference methods from another assembly?

Misc

  • types are encoded as strings -- probably not a great sign that they chose the "stringly-typed" road instead of using MD tokens ;-;
  • attribute ctors are called lazily when introspecting (e.g. typeof(C).GetCustomAttributes() will run the ctors of all attributes on class C)
  • getting RVA from MethodBase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment