Any limit to number of properties on a .NET Class?
I do not think the accepted answer is correct. We have just hit a problem with our assembly that adding any new property does build the code, but trying to load the assembly with reflection results in File is corrupt. HRESULT 0x8013110E
Removing any property after that allows to load the assembly with reflection again.
I obtained the metadata stats with ildasm:
C:\temp> ildasm.exe xyz.dll /stats /out=c:\temp\1.txt
C:\temp> cat C:\temp\1.txt | sls '\bFieldDef\b'
// FieldDef - 65534 (655340 bytes) 181 constant
Looks like a pretty clear 16 bits limit on FieldDef.
The metadata can have up to 24-bit references/definitions per assembly. Being a property, you need 2 methods per property. Hence the limit will be 23-bit, or 1 << 23 - 1
for the entire assembly.
Update:
If they are only read-only properties, the limit would be 1 << 24 - 1
.
Answer to second question:
No, there will be no performance overhead. Simple properties are likely to be inlined by the JIT.
Some thoughts:
You will never reach the above limit. Imagine having 16 million properties. That will require 16 million strings stored for the names too. Say the average name is 8 chars, then you are looking at a string table size of ~256MB (property name + method name), and then you havent even started coding yet. Just a thought.