Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A C# DLL doesn't work when used on vb.net project

Tags:

c#

vb.net

I'm using vb.net 2013. I try to use a tool that I have installed using NUGET. EntityFramewrok.Utilities / link : https://github.com/MikaelEliasson/EntityFramework.Utilities This is an open source dll and the whole code can be downloaded. From this DLL , I'm trying to use the "IncludeEFU" method. In the link above , is a code that I use on C# project and works :

var result = db.Contacts
.IncludeEFU(db, x => x.PhoneNumbers
.Where(n => n.Number == "10134")
.OrderBy(p => p.ContactId)
.ThenByDescending(p => p.Number))
.ToList();

I try to use on my VB.net application the same code like this :

Dim result = db.Contacts _
.IncludeEFU(db, Function(x) x.PhoneNumbers _
.Where(Function(n) n.Number = "10134")_
.OrderBy(Function(p) p.ContactId) _
.ThenByDescending(Function(p) p.Number)).ToList()

But I'm getting an error :

An unhandled exception of type 'System.ArgumentException' occurred in     EntityFramework.Utilities.dll

Additional information: Could not find a MemberExpression

Inspecting one by one the files in the dll's project ( that can be downloaded on the link ) , I see that the error message that I'm getting come from this sub :

private static PropertyInfo SetCollectionModifiersAndGetChildProperty<T, TChild>(Expression<Func<T, IEnumerable<TChild>>> collectionSelector, List<MethodCallExpression> childCollectionModifiers)
        where T : class
        where TChild : class
    {
        var temp = collectionSelector.Body;
        while (temp is MethodCallExpression)
        {
            var mce = temp as MethodCallExpression;
            childCollectionModifiers.Add(mce);
            temp = mce.Arguments[0];
        }
        childCollectionModifiers.Reverse(); //We parse from right to left so reverse it
        if (!(temp is MemberExpression))
        {
            throw new ArgumentException("Could not find a MemberExpression", "collectionSelector");
        }

        var childProp = (temp as MemberExpression).Member as PropertyInfo;
        return childProp;
    }

Look at the line :

throw new ArgumentException("Could not find a MemberExpression", "collectionSelector");

Why this is working on a C# project , and produce this error on a VB.net project ? How can I resolve this problem ? Thank you !

Edited : I try to make some changes in that sub :

    ...

    while (temp is MethodCallExpression)
            {
                var mce = temp as MethodCallExpression;
                childCollectionModifiers.Add(mce);
                temp = mce.Arguments[0];
            }
        while (temp is UnaryExpression)
        {
            var ue = temp as UnaryExpression;
            temp = ue.Operand;
        }
        .....

After I rebuild the dll file , and now the error message is disappear. But can anyone confirm that this is a correct solution ?

like image 883
alex Avatar asked Feb 02 '15 21:02

alex


1 Answers

The original EntityFramework.Utilities DLL may have been built with a different .NET "Target framework" than that of your VB.NET application.

This may explain why the original DLL did not work while your rebuilt DLL (using the same .NET "Target framework" defined for your project in VS.NET 2013) did work.

To examine the "Target framework" for VS.NET, right click on the applicable project in the Solution Explorer, then click on the "Properties" option. The "Application" tab will display a "Target framework:" label with a pulldown input of the .NET Framework version numbers available. The current setting of this pulldown is the .NET "Target framework" used to make your build in VS.NET 2013.

like image 113
JohnH Avatar answered Oct 31 '22 21:10

JohnH