Archived

This forum has been archived. Please start a new discussion on GitHub.

Partial classes and user generated constructors

Hi,

I'll begin with some example code:

Slice:
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)
{
val1=_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.

Cheers,
Rob.

Comments

  • xdm
    xdm La Coruña, Spain
    Hi,

    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)
    {
            try
            {
                return Activator.CreateInstance(t);
            }
            catch(MemberAccessException)
            {
                return null;
            }
    }
    

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

    Thanks,
    Jose