0

I am getting a System.TypeInitializationException in C# when i try to call the following:

List<BuyShopItem> buyShopItemList = new List<BuyShopItem>(0);

BuyShopItem is in an external assembly and contains the following:

namespace GameProtocol
{
    public struct BuyShopItem
    {
        public int ShopItemID;
        public int Amount;
        public int GoldPrice;
        public int SilverPrice;
        public int CharacterPointPrice;
        public int ResearchPointPrice;
    }
}

It's probably because of the external assembly, right?

Unfortunately, i cannot change it as i need to pass the BuyShopItem back again to another external Assembly.

Some information about the assembly: It's from a Unity game, .NET 3.5 (according to DotPeek: msil, .Net Framework v3.5)

I'm having the issue in SharpDevelop as well as Visual Studio 2017, so it probably not IDE-related. Result of peverify:

Microsoft (R) .NET Framework PE Verifier. Version  4.0.30319.0
Copyright (c) Microsoft Corporation. Alle Rechte vorbehalten.

[MD]: Error: Field has a duplicate, token=0x040059d7. [Token:0x040059CF]
[MD]: Error: Field has a duplicate, token=0x040059cf. [Token:0x040059D7]
[MD]: Error: Field has a duplicate, token=0x0400a48b. [Token:0x0400A478]
[MD]: Error: Field has a duplicate, token=0x0400a478. [Token:0x0400A48B]
4 Fehler wird/werden überprüft Assembly-CSharp.dll

If you have any Hints of what it could be, please tell me. I will try it out as soon as i can.

Here is a screenshot of the Exception in Visual Studio 2017: https://i.stack.imgur.com/JEySP.png

Update: I just tried the following: Console.WriteLine(typeof(BuyShopItem));, same error occured. Why isn't it possible to get the type?

Timon M
  • 11
  • 2
  • 3
    Does the exception have an `InnerException` or another message? `TypeInitializationException` means there was an exception on the type's constructor. Does it work if you do `var x = new ButShopItem()`? – Camilo Terevinto Apr 01 '18 at 17:26
  • What is the message you are getting in exception? – Chetan Apr 01 '18 at 17:26
  • 1
    A list with zero items it it seems pretty pointless. Either leave the parameter off or use a number greater than zero. – NightOwl888 Apr 01 '18 at 17:27
  • Have to tried `List buyShopItemList = new List();` Assuming your BuyShopItem is complete, it doesn't have a contructor that takes an int – Dweeberly Apr 01 '18 at 17:27
  • @Camilo Terevinto InnerException {"Die Common Language Runtime hat ein ungültiges Programm gefunden."} System.Exception {System.InvalidProgramException} – Timon M Apr 01 '18 at 17:30
  • @NightOwl888 I will be adding entries, after i got the List created. But for debugging purposes, i commented that out – Timon M Apr 01 '18 at 17:31
  • @TimonM Sorry, I don't speak German. Mind translating that? – Camilo Terevinto Apr 01 '18 at 17:32
  • @Dweeberly Same error with that. – Timon M Apr 01 '18 at 17:32
  • @Camilo Terevinto Of course, sorry. It's saying: {"Common Language Runtime found an invalid program."} System.Exception {System.InvalidProgramException} – Timon M Apr 01 '18 at 17:33
  • I added a screenshot of the exception: https://i.imgur.com/WHCbWTM.png – Timon M Apr 01 '18 at 17:37
  • Probably a 64 bit/32 bit issue. See https://www.codeproject.com/Articles/383138/BadImageFormatException-x-i-x – Chris Shain Apr 01 '18 at 18:20
  • @Chris Sahin according to the post i should set my project to x86, which i did. The thing is: It is no complicated class. I can use other classes from the same assembly without any problems. But i will try around some 32/64/Any CPU options and report back if i get something. I'll also try CorFlags. – Timon M Apr 01 '18 at 19:16
  • Both (my executable and the external assembly) are x86, only the assembly has no "32BITREQ" set in CorFlags – Timon M Apr 01 '18 at 19:30
  • Is the dll referencing any additional dlls that could be missing from your project directory? – ZiggZagg Apr 02 '18 at 16:45

1 Answers1

0

It is OK to create a zero-length list of structs.

It is also OK to create a list of some type that is defined in a different assembly - but check to make sure that the necessary DLL can be located at runtime.

Instances of structs are generally fairly safe to construct - but check to see if there is a custom constructor that is throwing an exception.

Also carefully check to see if the BuyShowItem struct specifies any static fields that call into other code, which in turn may be failing. This is often the root cause of TypeInitializationException.

See: https://msdn.microsoft.com/en-us/library/system.typeinitializationexception(v=vs.110).aspx

Peter Aylett
  • 750
  • 3
  • 8
  • Thank you for your answer, but if i copy the exact same code (which is the same as i wrote in the question, except i stripped some comments of dotPeek out) in my own code it works. So it can't be a code issue, it is some kind of issue with the assembly. I have some kind of Workaround now, but it's not the only part where this error occurs. As you can see in the code, the struct contains normal integer values and no Constructor. – Timon M Apr 02 '18 at 15:23