[C#] [SOLVED] Making usage of Inheritance (override method) leads to System.BadImageFormatException

Currently I’m encountering a strange issue where an Exception of type “System.BadImageFormatException” is thrown for an overridden method.
Basically I am trying to extend one of NativeUI’s MenuItems, in particular the UIMenuSliderItem.
I double checked that Platform Target is set to “ANY CPU”, both for NativeUI as well as for my script and that both of them are targeting .NET Framework 4.5.2
The code throwing that exception is as follows:
HeritageSliderItem.cs

 public class HeritageSliderItem : UIMenuSliderItem
    {
        private readonly Color disabledColor = Color.FromArgb(163, 159, 148);

        private readonly Sprite maleSprite = new Sprite("mpleaderboard", "leaderboard_male_icon", new Point(0, 0), new Size(40, 40));
        private readonly Sprite femaleSprite = new Sprite("mpleaderboard", "leaderboard_female_icon", new Point(0, 0), new Size(40, 40));

        public HeritageSliderItem(string text, string description)
        : base(text, description, true)
        {
        }

        public override void Draw()
        {
            base.Draw();

            var offset = 176 + Offset.X + _rectangleBackground.Size.Width - _rectangleSlider.Size.Width;
            this.femaleSprite.Position = new Point((int)offset - 40, _badgeLeft.Position.Y);
            this.femaleSprite.Color = Enabled ? (Selected ? Color.Black : Color.WhiteSmoke) : this.disabledColor;
            this.femaleSprite.Draw();
            this.maleSprite.Position = new Point(400 + (int)Offset.X + Parent.WidthOffset, _badgeRight.Position.Y);
            this.maleSprite.Color = Enabled ? (Selected ? Color.Black : Color.WhiteSmoke) : disabledColor;
            this.maleSprite.Draw();
        }
    }

TestScript.cs

public class TestScript : BaseScript
    {
        private readonly UIMenu testMenu = new UIMenu("Test Menu", string.Empty);
        private readonly HeritageSliderItem testItem = new HeritageSliderItem("Test Item", string.Empty);

        private readonly MenuPool menuPool = new MenuPool();

        public TestScript()
        {
            this.testMenu.AddItem(this.testItem);
            this.menuPool.Add(this.testMenu);
            this.Tick += this.OnTick;
        }

        private async Task OnTick()
        {
            this.testMenu.Visible = true;
            this.menuPool.ProcessMenus();
        }
    }

The detailed error log:

[     48610] Failed to run a tick for TestScript: System.BadImageFormatException: Error verifying FiveM.Test.HeritageSliderItem:Draw (): Cannot load method from token 0x0a000025 for call at 0x0087

[     48610]   at NativeUI.UIMenu.Draw () [0x0029e] in C:\Users\Nebuchadnezzar\Downloads\NativeUI-fivem\NativeUI\UIMenu.cs:1111 

[     48625]   at NativeUI.MenuPool.Draw () [0x00023] in C:\Users\Nebuchadnezzar\Downloads\NativeUI-fivem\NativeUI\MenuPool.cs:215 

[     48625]   at NativeUI.MenuPool.ProcessMenus () [0x0000c] in C:\Users\Nebuchadnezzar\Downloads\NativeUI-fivem\NativeUI\MenuPool.cs:237 

[     48625]   at FiveM.Test.TestScript+<OnTick>d__4.MoveNext () [0x00013] in <9e112501c6e5444da83249da956e9f2f>:0 

For further investigation I’ve attached a minimal working example.
FiveM-Test.zip (1.8 MB)

The script’s resource lua looks like this.
__resource.lua

resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

client_scripts {
    'FiveM.Test.net.dll'
}

files {
    'NativeUI.net.dll'
}

Does anyone know what is wrong with the code and/or what is going on under the hood? Because I have similar code based on Script Hook V .NET which is working fine. Any help or clarification would be greatly appreciated.

Greetings!

PS: Please move this topic, if the Category shouldn’t be the right one. I am still unsure about this.

Edit: I’ve attached a “working” resource containing the compiled script.
nativeui-test.zip (54.6 KB)

1 Like

Can you attach the compiled script

I’ve edited the post accordingly.

Cannot load method from token 0x0a000025
That token is referring to get_WhiteSmoke. Looks like FiveM’s System.Drawing doesn’t contain the known colors table. Use Color.FromArgb(-657931) instead for WhiteSmoke and Color.FromArgb(-16777216) for Black.

3 Likes

Wow!
Many, many thanks. It works :slight_smile: