Home Bug Reports

Partial classes and user generated constructors

rob6676rob6676 Member Rob DuffOrganization: L-3 NautronixProject: Maritime Domain Awareness

I'll begin with some example code:

class A
int ex1;
double ex2;
class B extends class A
["protected"] long val1;

user implementation of c# partial class
public partial class B
public B(long _val1)

I'm getting System.Reflection.TargetParameterCountException on some builds, but not all. This made for some interesting debugging.

After stepping in to AssemblyUtil.cs, public static object createInstance(Type t), on line 216, the invocation of the constructor is passed an object array of zero length:
return t.GetConstructor(constructor).Invoke(new object[]{});
which would suggest the default constructor, however, in my case, the default constructor isn't always the first in the ConstructorInfo array.

As per the microsoft documentation in:
Type.GetConstructors Method (System)
It states that "The GetConstructors method does not return constructors in a particular order, such as declaration order. Your code must not depend on the order in which constructors are returned, because that order varies."

This probably explains why some builds would work and others didn't.



  • xdmxdm La Coruña, SpainAdministrators, ZeroC Staff Jose Gutierrez de la ConchaOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff

    Thanks for reporting this issues and track it down.

    We are internally discussing how to better support/fix that. meanwhile you can replace AssembleyUtil.createInstace method implementation by:
    public static object createInstance(Type t)
                return Activator.CreateInstance(t);
                return null;

    I was able to reproduced the problem, and test that the fix solve the issue.

Sign In or Register to comment.