Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net 6 fails with exception Multiple methods found with 2 parameter(s) #222

Closed
RobertLivermore opened this issue Mar 30, 2022 · 5 comments
Closed
Labels

Comments

@RobertLivermore
Copy link

There is a reflection exception thrown from AgileObjects.NetStandardPolyfills library. "Multiple methods found with 2 parameter(s)". The problem was introduced when updating to net6. Not sure what the right combination of nuget packages to use to get this to work.

I created a sample in dotnetfiddler to demonstrate
Fiddlier Example

using System;
using System.Collections.Generic;
using AgileObjects.AgileMapper.Members;
using Agile = AgileObjects.AgileMapper;
#nullable enable

public class Program
{
	public static void Main()
	{
		var mapper = Agile.Mapper.CreateNew();
		mapper.WhenMapping
			.FromDictionaries
			.ToANew<Doctor>()
			.PassExceptionsTo(LogMappingException);

		var readerResult = new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase)
		{
			{"Name", "Steve"},
			{"PhoneNumbers[0]", "01234 567890"},
			{"PhoneNumbers[1]" , "07890 654321"},
			{"PhoneNumbers[2]",  "01234 987654"},
			{"Specialties[0]Id",  123},
			{"Specialties[0]Name",  "Emergency Medicine"},
			{"Specialties[1]Id",  456},
			{"Specialties[1]Name",  "Critical Care"},
		};

		 Console.WriteLine( mapper.GetPlanFor<IDictionary<string, object?>>().ToANew<Doctor>());
		var doctor = mapper.Map(readerResult).ToANew<Doctor>();

		Console.Write(doctor);
		Console.ReadLine();
	}
	static void LogMappingException(IMappingExceptionData<IDictionary<string, object?>, Doctor> problem)
	{
		Console.WriteLine(problem);
	}	
}
public class Doctor
{
    public string? Name { get; set; }
    public string[] PhoneNumbers { get; set; }
    public System.Collections.Generic.IReadOnlyCollection<Specialty>? Specialties { get; set; }
}

public class Specialty
{
    public int Id { get; set; }
    public string? Name { get; set; }
}

Stack Trace

Unhandled exception. System.TypeInitializationException: The type initializer for 'AgileObjects.AgileMapper.DataSources.DictionaryEntryVariablePair' threw an exception.
 ---> System.Reflection.AmbiguousMatchException: Multiple methods found with 2 parameter(s)
   at AgileObjects.NetStandardPolyfills.MethodExtensionsPolyfill.WithParameterCount(IEnumerable`1 methods, Int32 parameterCount)
   at AgileObjects.NetStandardPolyfills.MethodExtensionsPolyfill.GetPublicStaticMethod(Type type, String name, Int32 parameterCount)
   at AgileObjects.AgileMapper.DataSources.DictionaryEntryVariablePair..cctor()
   --- End of inner exception stack trace ---
   at AgileObjects.AgileMapper.DataSources.DictionaryEntryVariablePair.GetMatchingKeyAssignment(Expression targetMemberKey)
   at AgileObjects.AgileMapper.DataSources.DictionaryEntryVariablePair.GetMatchingKeyAssignment()
   at AgileObjects.AgileMapper.DataSources.DictionaryEntryDataSource.GetMatchingKeyExistsTest()
   at AgileObjects.AgileMapper.DataSources.DictionaryEntryDataSource.FinalisePopulationBranch(Expression alternatePopulation, IDataSource nextDataSource, IMemberMapperData mapperData)
   at AgileObjects.AgileMapper.Members.Population.MemberPopulator.GetPopulation()
   at AgileObjects.AgileMapper.ObjectPopulation.ComplexTypes.MultiStatementPopulationExpressionFactory.GetPopulationExpressionsFor(IMemberPopulator memberPopulator, IObjectMappingData mappingData)+MoveNext()
   at AgileObjects.AgileMapper.ObjectPopulation.ComplexTypes.PopulationExpressionFactoryBase.GetPopulationsAndCallbacks(IObjectMappingData mappingData)+MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at AgileObjects.AgileMapper.ObjectPopulation.ComplexTypes.PopulationExpressionFactoryBase.AddPopulation(MappingCreationContext context)
   at AgileObjects.AgileMapper.ObjectPopulation.ComplexTypes.ComplexTypeMappingExpressionFactory.AddObjectPopulation(MappingCreationContext context)
   at AgileObjects.AgileMapper.ObjectPopulation.MappingExpressionFactoryBase.<>c__DisplayClass11_0.<AddPopulationsAndCallbacks>b__0(MappingExpressionFactoryBase factory, MappingCreationContext ctx)
   at AgileObjects.AgileMapper.ObjectPopulation.MappingExpressionFactoryBase.AddPopulationsAndCallbacks[TArg](TArg argument, MappingCreationContext context, Action`2 mappingBodyPopulator)
   at AgileObjects.AgileMapper.ObjectPopulation.MappingExpressionFactoryBase.AddPopulationsAndCallbacks(MappingCreationContext context)
   at AgileObjects.AgileMapper.ObjectPopulation.MappingExpressionFactoryBase.Create(IObjectMappingData mappingData)
   at AgileObjects.AgileMapper.DataSources.ComplexTypeDataSource.Create(IObjectMappingData mappingData)
   at AgileObjects.AgileMapper.DataSources.Factories.Mapping.ComplexTypeMappingDataSourceFactory.CreateFor(IObjectMappingData mappingData)
   at AgileObjects.AgileMapper.DataSources.Factories.Mapping.MappingDataSourceSetFactory.CreateFor(IObjectMappingData mappingData)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMapperFactory.Create[TSource,TTarget](ObjectMappingData`2 mappingData)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingData`2.GetOrCreateMapper()
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMapperFactory.<>c__7`2.<GetOrCreateRoot>b__7_0(IRootMapperKey key)
   at AgileObjects.AgileMapper.Caching.ArrayCacheBase`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMapperFactory.GetOrCreateRoot[TSource,TTarget](ObjectMappingData`2 mappingData)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingData`2..ctor(TSource source, TTarget target, Nullable`1 elementIndex, Object elementKey, MappingTypes mappingTypes, IMappingContext mappingContext, IObjectMappingData declaredTypeMappingData, IObjectMappingData parent, Boolean createMapper)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingData`2..ctor(TSource source, TTarget target, Nullable`1 elementIndex, Object elementKey, MappingTypes mappingTypes, IMappingContext mappingContext, IObjectMappingData parent, Boolean createMapper)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingDataFactory.ForRootFixedTypes[TSource,TTarget](TSource source, TTarget target, MappingTypes mappingTypes, IMappingContext mappingContext, Boolean createMapper)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingDataFactory.ForRootFixedTypes[TSource,TTarget](TSource source, TTarget target, IMappingContext mappingContext, Boolean createMapper)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingDataFactory.ForRootFixedTypes[TSource,TTarget](IMappingContext mappingContext, Boolean createMapper)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingDataFactory.ForRootFixedTypes[TSource,TTarget](IMappingContext mappingContext)
   at AgileObjects.AgileMapper.Api.PlanTargetSelector`1.GetMappingPlan[TTarget](IMappingContext planContext, Func`2 mappingDataFactory, ICollection`1 configurations)
   at AgileObjects.AgileMapper.Api.PlanTargetSelector`1.GetMappingPlan[TTarget](MappingRuleSet ruleSet, ICollection`1 configurations)
   at AgileObjects.AgileMapper.Api.PlanTargetSelector`1.ToANew[TResult](Expression`1[] configurations)
   at Program.Main()
Command terminated by signal 6
@SteveWilkes
Copy link
Member

Thanks for letting me know about this - I discovered it recently myself and have a fix in the branch for the next release.

Cheers,

Steve

@SteveWilkes SteveWilkes added bug in-branch A feature or bug fix exists in code, and a release will follow labels Mar 30, 2022
@RobertLivermore
Copy link
Author

Thanks for the quick reply. What is the time frame for the next release? Or is there any alpha or beta releases to work with.

👍I like the AgileObject.Mapper better than Automapper because the AgileMapper expression tree throws better exceptions. Which are faster to debug and maintain. --Comparing dictionary Mapping between libraries. When AgileMapper throws exception I know which property failed, AutoMapper just gives a reflection error nearly impossible to find the failing property.

@SteveWilkes
Copy link
Member

Thanks for the feedback - happy to know you're finding the project useful!

This issue is fixed as of v1.8.1, which is now available on NuGet. Cheers!

@SteveWilkes SteveWilkes removed the in-branch A feature or bug fix exists in code, and a release will follow label Mar 31, 2022
@RobertLivermore
Copy link
Author

Tested v1.8.1. It works with net 6. A big thank you.
ThankYouThanksGIF

@SteveWilkes
Copy link
Member

You're welcome! Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants