This project has moved and is read-only. For the latest updates, please go here.


Supplied properties in parameter Properties must be of the same type as T


First of all I'd like to thank you for this great plugin.

In the following file,
the following method,
public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders, TableStyles TableStyle, BindingFlags memberFlags, MemberInfo[] Members)
There is this following code which is throwing an exception when passing a dynamic object T with known MemberInfos.
var type = typeof(T);
if (Members == null)
    Members = type.GetProperties(memberFlags);
    foreach (var t in Members)
        if (t.DeclaringType != type)
            throw (new Exception("Supplied properties in parameter Properties must be of the same type as T"));
Currently I have commented this code and importing the whole source code in my solution. If the 'else' part is commented out all is working well even with dynamic objects.

Do you think you could comment the else part and update the nuget? if not why do you have to check the types of individual properties? This would not allow dynamic objects to be passed using dynamic keyword.

Please let me know.

Thank you.
Closed Jan 14, 2015 at 10:31 PM by swmal


swmal wrote Jan 13, 2015 at 1:25 PM

This has been changed in the latest code to the following:
if (t.DeclaringType!=null && t.DeclaringType != type && !t.DeclaringType.IsSubclassOf(type))
                        throw new InvalidCastException("Supplied properties in parameter Properties must be of the same type as T (or an assignable type from T");
Please let me know if that works better for you.

kslesinsky wrote May 13, 2015 at 11:27 PM

I have a similar situation, and this doesn't quite fix it. I have a base class with property X. The collection I'm passing in is of type T which is a derived class. I'm passing in property X as one of the members.

Can you add another check to the if statement... something like:

&& !type.IsSubclassOf(t.DeclaringType)

BobCac wrote Jan 28 at 12:58 AM

Hi guys,

First of all thanks for the great tools for working with Excel, it works great. However i've encountered the same issue as kslesinsky in one of my case scenarios.

When dealing with derived classes and properties, having for example:
  • Base class BaseClass, with inheritable properties BasePropA, BasePropB
  • Derived class DerivedClass which inherits BaseClass
In the fragment of code cited here, when passing a collection of type DerivedClass, type would be typeof(DerivedClass), and in the for first loop taking t as its member BasePropA, results in that t.DeclaringType is typeof(BaseClass) (since BaseClass is the class in which BasePropA was declared).

Thus, when evaluating t.DeclaringType.IsSubclassOf(type) the code is checking whether BaseClass is a subclass of DerivedClass, which it obviously is not (the relation is the opposite) and hence ending up throwing an exception. This exception is not allowing us to pass inherited public instance properties that belong to the generic type of the IEnumerable parameter to be included in the Excel worksheet.

With the above being said, I and probably a bunch of other people using the library would greatly appreciate it if this gets fixed and uploaded to nuget in the future.

Thanks for taking the time to read this and hopefully fixing the issue.

PS: I think you can benefit from kslesinsky's code suggestion to get started.