0

I am having trouble with extension methods. Another method was causing the same error, now I am having the same issue again. While compiling - visual studio is marking my extension method as ambiguous. But I am pretty sure this is the only implementation I have -

The extension method I have is -

public static class TypeInfoExtensions
{
    /// <summary>
    /// Gets the property dictionary.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="types">The types.</param>
    /// <returns></returns>
    public static IEnumerable<Dictionary<string, object>> GetPropertyDictionaryList<T>(this IEnumerable<TypeInfo> types) where T : Attribute
    {
        return types.Select(x => x.GetPropertyDictionary<T>());
    }
    /// <summary>
    /// Gets the property dictionary.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="type">The type.</param>
    /// <returns></returns>
    public static Dictionary<string, object> GetPropertyDictionary<T>(this TypeInfo type) where T : Attribute
    {
        var dict = new Dictionary<string, object>();

        return dict;
    }
}

And it is used like this -

var model = ...... // other codes

var parameters = Assembly.GetAssembly(typeof (Model.Domain.Tenants.Tenant)).DefinedTypes;
model.AvailableProperties = parameters.GetPropertyDictionaryList<MessageTemplateItem>().ToList()

This is the only implementation I have, I just added it, but VS is showing this error -

Error 81 The call is ambiguous between the following methods or properties: 'SMP.Core.Extensions.TypeInfoExtensions.GetPropertyDictionary(System.Reflection.TypeInfo)' and 'SMP.Core.Extensions.TypeInfoExtensions.GetPropertyDictionary(System.Reflection.TypeInfo)'

I am thinking this might be a temporary error, may be cleaning the solution is not cleaning references or cache. But if any body else have faced this error, I would very much appreciate your feedback.

UPDATE Strangely enough this code works -

public static IEnumerable<Dictionary<string, object>> GetPropertyDictionaryList<T>(this IEnumerable<TypeInfo> types) where T : Attribute
{
    return types.Select(x => GetPropertyDictionary<T>(x));
}

I am really confused now, so far I knew these are the same for an extension method -

GetPropertyDictionary<T>(x)
x.GetPropertyDictionary<T>()

Any ideas guys?

brainless coder
  • 6,310
  • 1
  • 20
  • 36

2 Answers2

1

I had the same issue. I had declared an extension method in project A. While using it in this project worked, it would not work in another project B. There it was marked as being ambiguous. Calling it in a static way in project B worked however.

When searching by string in the whole solution or reference, I could not find any additional implementation to the one in project A.

The problem finally was: I accidentally added a link of the source file of the extension method from project A into project B. Removing the link to the source file in project B solved the problem.

Fabian
  • 1,100
  • 11
  • 14
  • Please comment the down-vote, I would like to improve the answer if something is wrong or not understandable, but this is only possible if I get more information. – Fabian Nov 27 '17 at 17:46
0

I just ran across the same issue. If it wasn't for source control I'd never have tracked it down.

The project had been using extension methods just fine for a number of years. Then, after some changes all the uses of extension methods started coming up as:

The call is ambiguous between the following methods or properties: ...

Both items called out had exactly the same namespace and signature.

A diff on the csproj file revealed the problem. The output executable from the obj directory had somehow been added as a reference in the hint path.

Something like:

<ItemGroup>
    <Reference Include="OutputExecutable">
        <HintPath>obj\DEBUG\OutputExecutable.exe</HintPath>
    </Reference>
</ItemGroup>

Removing that erroneous entry got everything building again. How it got there in the first place is currently a mystery.

Daniel Ballinger
  • 13,187
  • 11
  • 69
  • 96