Skip to content

mnemonic-bit/MemoryAddressHelpers

Repository files navigation

MemoryAddressHelpers

This library provides two methods which reveal the actual memory address of an instance of a class or value object, i.e. the location of a reference type is where the reference points at, while the location of a value type is the address of the variable that holds that value.

This package is for debugging purposes only, and should never be included in release code.

Why Do I Need This Library?

Since this library has only two main methods, its a valid question why exactly its not easier to directly drop those methods into that project where they are needed.

There are two reasons for this. First of all, we should not use a direct memory location and perform any kind of magic trick on that. This methods should only be used to make debugging easier for really low level code and code-generation, like IL code that has been generated by your program.

Second, for this methods to work, we need unsafe code, which you normally do not want to activate in your own project. By placing this methods into a Nuget of their own, we can leave our own project untouched (except another package reference to this Nuget package). There is no need to use the tag AllowUnsafeBlocks in your own project.

How Do I Add This Library To My Project?

Since its not exactly straight forward to add this Nuget, here are a few hints to make it as easy as possible for you to add the MemoryAddressHelpers Nuget to your project.

You can use the Nuget Package Manager in Visual Studio, or use the command-line and type the following command

dotnet add package MemoryAddressHelpers

Either way, you will get a Nuget reference in you .csproj file, which will look like this one

  <ItemGroup>
    <PackageReference Include="MemoryAddressHelpers" Version="1.1.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

The problem with this entry is, that the IncludeAssets value compile is missing, hence no symbol of the MemoryAddressHelpers pacakge will be available in your own project. To make this work, simply change or add to the value of IncludeAssets-tag to you liking, or at least to reflect this:

  <ItemGroup>
    <PackageReference Include="MemoryAddressHelpers" Version="1.1.1">
      <IncludeAssets>compile</IncludeAssets>
    </PackageReference>
  </ItemGroup>

or simply skip all of this options and just include the package without any additional options set:

  <ItemGroup>
    <PackageReference Include="MemoryAddressHelpers" Version="1.1.1"/>
  </ItemGroup>

Examples Using Extension Methods

To get the address of a class, we can use the GetMemoryAddress() extension methods. For all examples to work, you will need the following using statement

using MemoryAddressHelpers;

The current address of a reference-type (e.g. a class) will be returned by:

var someClass = new SomeClass();
var address = someClass.GetMemoryAddress();

Value types are a bit trickier, though, because we must take care to pass a reference instead of a copy of that object. The following example shows how to get the stack address of a value type:

var someValue = 42;
var address = someValue.GetMemoryAddress();

or for that matter, even complex value types, i.e. structs:

var someStruct = new SomeStruct();
var address = someStruct.GetMemoryAddress();

Examples Using Static Methods

All of the examples shown above can be rewritten by using direct method calls, instead of using extension methods.

The current address of a reference type (e.g. a class), use:

var someClass = new SomeClass();
var longAddress1 = MemoryAddress.Get(someClass);

For a value-type you need to use the ref-keyword, otherwise the value will be copied, and the Get() method would only return the location of that value on the call-stack:

var someStruct = new SomeStruct();
var address = MemoryAddress.Get(ref someStruct);

About

C# methods which help with memory addresses.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages