Skip to content

Accessing private class members (Reflection)

Linniel Driver-Williams edited this page May 17, 2026 · 5 revisions

(Page under construction)


Important

PRIVATE CLASS MEMBERS ARE USUALLY PRIVATE FOR A REASON. ONLY TRY TO ACCESS THEM IF THERE IS NO OTHER WAY.

That said, if there is no other way, this page outlines the 3 key ways of accessing private class members (eg. private fields, methods) from your code.

AccessTools

FieldRef

Field/Method

Notes on backing fields

Publicizer

Publicizer is a library that allows you to access private fields via at compile time.

If you have having red arrows after adding a new publicized field, clean your solution and then rebuild. (in Rider: click Build -> Clean Solution)

Publicize target

    <PropertyGroup>
        <PublicizerClearCacheOnClean>true</PublicizerClearCacheOnClean>
    </PropertyGroup>
    <ItemGroup>
        <Publicize Include="sts2:MegaCrit.Sts2.Core.Commands.Builders.AttackCommand._combatState"/>
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Alchyr.Sts2.BaseLib" Version="*" PrivateAssets="All"/>
        <PackageReference Include="Alchyr.Sts2.ModAnalyzers" Version="*"/>
        <PackageReference Include="Krafs.Publicizer" Version="2.3.0">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>

Publicize all

Traverse

Clone this wiki locally