Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous Type Name Collision

A linq query that returns an anonymous type throws the following error when executed.

The type '<>f__AnonymousType9<Name,Value>' exists in both    
'Customer.CustomerWeb.Presentation.dll' and 'Customer.CustomerContext.dll'

Using JetBrains dotPeek I was able to find that there is are 2 compiler generated classes that collide.

Customer.CustomerContext.dll

 internal sealed class <>f__AnonymousType9<<PayrollSiteID>j__TPar, <IsActive>j__TPar>

Customer.CustomerWeb.Presentation.dll

 internal sealed class <>f__AnonymousType9<<Name>j__TPar, <Value>j__TPar>

Both of the generated classes are in the root namespace. Is there any way I can direct Anonymous Type classes to a particular namespace on each assembly? The easy fix is to add a third variable to one of the anonymous queries, however this is more of a hack.

like image 320
Chad Carisch Avatar asked May 14 '14 13:05

Chad Carisch


People also ask

How do you define anonymous type?

Anonymous types are class types that derive directly from object , and that cannot be cast to any type except object . The compiler provides a name for each anonymous type, although your application cannot access it.

What are anonymous types in C#?

Anonymous types in C# are the types which do not have a name or you can say the creation of new types without defining them. It is introduced in C# 3.0. It is a temporary data type which is inferred based on the data that you insert in an object initializer.

When specifying multiple properties use an anonymous type?

When specifying multiple properties use an anonymous type: C#: 't => new { t. MyProperty1, t. MyProperty2 }' VB.Net: 'Function(t) New With { t. MyProperty1, t.


1 Answers

I think you want something like the following, where you add your using statement inside the namespace to give it preference in name resolution:

using Customer.CustomerContext;
namespace yourNameSpace
{
    using Customer.CustomerWeb.Presentation; //Where f__AnonymousType9<Name,Value> exists
}
like image 85
n4gy3 Avatar answered Sep 27 '22 22:09

n4gy3