I need to map IReadOnlyList<Person> to IReadOnlyList<PersonResponse> using Automapper.
IReadOnlyList<Person> personList = await _personRespository.getall();
var t = MyMapper.Mapper.Map<IReadOnlyList<PersonResponse>>(personList );
Mapping Class
CreateMap<IReadOnlyList<PersonResponse>, IReadOnlyList<Person>>().ReverseMap();
The error I get:
System.TypeLoadException: Method 'get_Item' in type
'Proxy_System.Collections.Generic.IReadOnlyList`1[[App.Application.Responses.PersonResponse,
App.Application, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]34471389' from assembly 'AutoMapper.Proxies,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005'
does not have an implementation. at
System.Reflection.Emit.TypeBuilder.CreateTypeNoLock() at
System.Reflection.Emit.TypeBuilder.CreateTypeInfo()
Create an mapping between Person and PersonResponse, the concerned element types. Automapper would take care of collections themselves.
CreateMap<PersonResponse, Person>().ReverseMap();
You can read more Automapper and Collections here
Related
Using the latest RxUI v8 preview and Splat 2.0, in a UWP project referencing a .Net Standard 2.0 library, I can't register my view and viewmodel unless they reside in the same assembly.
I have:
Locator.CurrentMutable.RegisterLazySingleton(() => new HomeView(), typeof(IViewFor<HomeViewModel>));
But Splat gives an error:
DefaultViewLocator: Failed to find type named 'RxUI.UWP.Core.Views.HomeView, RxUI.UWP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
DefaultViewLocator: Failed to resolve service for type 'ReactiveUI.IViewFor`1[[RxUI.UWP.Core.ViewModels.HomeViewModel, RxUI.UWP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
DefaultViewLocator: Failed to find type named 'ReactiveUI.IRoutableView, ReactiveUI, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null'.
DefaultViewLocator: Failed to resolve service for type 'ReactiveUI.IViewFor`1[[ReactiveUI.IRoutableViewModel, ReactiveUI, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
DefaultViewLocator: Failed to resolve view for view model type 'ReactiveUI.IRoutableViewModel'.
DefaultViewLocator: Failed to find type named 'RxUI.UWP.Core.Views.HomeView, RxUI.UWP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
So it's looking for the HomeView in the "Core" assembly, but it resides in the UWP project. Here's the structure...
I experienced the same issue with a similar environment as your. The issue resided in the DefaultViewLocator, since the view type is being renamed and resolved incorrectly to the viewmodel's assembly and namespace.
See lines 133-134 for how the view type name is determined:
var viewModelTypeName = viewModelType.AssemblyQualifiedName;
var proposedViewTypeName = this.ViewModelToViewFunc(viewModelTypeName);
Note: ViewModelToViewFunc is only a String.Replace that replaces "ViewModel" to "View" (see constructor).
To resolve this issue, my workaround was to create my own IViewLocator implementation, something like:
public class MyViewLocator : IViewLocator {
public MyViewModelLocator(Assembly viewAssembly, string viewNameSpace)
...
private IViewFor AttemptViewResolutionFor(Type viewModelType, string contract)
{
// proposed view type is now based on provided namespace + classname as modified by ViewModelToViewFunc
if (viewModelType == null) return null;
var viewModelTypeName = viewModelType.Name;
var proposedViewTypeName = _viewNamespace + "." + this.ViewModelToViewFunc(viewModelTypeName);
...
private IViewFor AttemptViewResolution(string viewTypeName, string contract)
{
try
{
// resolve view type in the assembly of the view, and not assembly of the viewmodel
var viewType = _viewAssembly.GetType(viewTypeName); // instead of Reflection.ReallyFindType(viewTypeName, throwOnFailure: false);
...
}
Finally, your custom viewlocator implementaiton must be registered with splat so that it overwrites DefaultViewLocator implementation:
Locator.CurrentMutable.RegisterConstant<IViewLocator>(new MyViewLocator(typeof(SplashView).Namespace, typeof(SplashView).Assembly));
I'm Using C#.
I'm trying to create a web method in my .asmx file as follow:
[WebMethod]
public string MyWebMethod(Dictionary<string, string> parameters)
{
//...
}
And I got the following exception:
The type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because it implements IDictionary.
How can i fix it?
Thanks.
you can't use Dictionary direct but you can use list as parameters
and this link can help you to convert List to Dictionaryand continue your flow
C# Convert List to Dictionary
I'm trying to (de)serialize a class with this simple piece of code:-
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
// Serialize
var json = JsonConvert.SerializeObject(obj, settings);
// Deseralize - this is where it fails
var test = JsonConvert.DeserializeObject<MyObject>(json, settings);
DeseralizeObject() fails with a JsonSerializationException:-
Error resolving type specified in JSON 'Xxx.Common.MyObject, Xxx.Common'. Path '$type', line 1, position 110
Inner exception: JsonSerializationException, message "Could not load assembly 'Xxx.Common".
I don't understand why it can't load the assembly - it's being referenced by this project!
It works if I don't use the JsonSerializerSettings, however I need this because the class being serialized will eventually have a List<SomeBaseClass> property that will contain derived types.
Any thoughts? I'm using Json.Net v6.0.
Edit:
I just tried adding TypeNameAssemblyFormat = FormatterAssemblyStyle.Full to my serializer settings, which resulted in this different (and confusing) exception message:-
Type specified in JSON 'Xxx.Common.MyObject, Xxx.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with 'Xxx.Common.MyObject, Xxx.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Path '$type', line 1, position 165.
Edit2:
To clarify things, the above code is a complete repro of the problem, and resides in a larger WPF application, while MyObject resides in a different project ("Xxx.Common") in the same solution, and is referenced by the WPF application project - I've simply replaced our company namespace with "Xxx" for this post.
MyObject is a simple POCO that I've created to rule out any issues that may be due to complex types, and consists of a few string and double properties, i.e.
public class MyObject
{
public string Name {get;set;}
public double Foo {get;set;}
...
}
The serialized JSON looks like this (again company NS replaced) - the pertinent part (i.e. the "$type") appears to be correct:-
{"$type":"Xxx.Common.MyObject, Xxx.Common","Name":null,"Foo":0.0,"StepSize":0.0,"Convergence":0.0,"Cutoff":0.0}
Why does var type = Type.GetType("System.Windows.Forms.TextBox"); return null?
I am trying to get the type of a TextBox type from a string.
You should include the full assembly name too:
var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
Note the documentation on MSDN (emphasis mine):
The assembly-qualified name of the type to get. ... If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.
So only mscorlib and the Assembly.GetExecutingAssembly() can be resolved using the type name only, else you need the full assembly name too.
You should also include the assembly name and public tokens, etc:
var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
I'm working on an ASP.NET MVC 3 application using AutoMapper 2.2.0. I have some AutoMapper profiles declared and when I initialize them manually everything works just fine.
AutoMapper.Mapper.Initialize(x =>
{
x.AddProfile<ProdutoToProdutoViewModel>();
x.AddProfile<IPagedListProdutoToIPagedListProdutoViewModel>();
x.AddProfile<ItemToItemViewModel>();
x.AddProfile<CarrinhoToCarrinhoViewModel>();
});
//This is working
But when I try to initialize them with Bootstrapper.AutoMapper 2.0.3.0...
Bootstrapper.With.AutoMapper().Start();
...a configuration exception is thrown:
The following property on eGuruShop.Web.ViewModels.ItemViewModel cannot be mapped:
Itens
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type eGuruShop.Web.ViewModels.ItemViewModel.
Context:
Mapping to property Itens from eGuruShop.Domain.CatalogoProdutos.Item to eGuruShop.Web.ViewModels.ItemViewModel
Mapping to property Itens from System.Collections.Generic.IList`1[[eGuruShop.Domain.CatalogoProdutos.Item, eGuruShop.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.IList`1[[eGuruShop.Web.ViewModels.ItemViewModel, eGuruShop.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Mapping from type eGuruShop.Domain.CatalogoProdutos.Carrinho to eGuruShop.Web.ViewModels.CarrinhoViewModel
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown
The CarrinhoToCarrinhoViewModel profile depends on the ItemToItemViewModel profile and when I change the initialization order to
AutoMapper.Mapper.Initialize(x =>
{
x.AddProfile<ProdutoToProdutoViewModel>();
x.AddProfile<IPagedListProdutoToIPagedListProdutoViewModel>();
x.AddProfile<CarrinhoToCarrinhoViewModel>();
x.AddProfile<ItemToItemViewModel>();
});
//Exception
I've got the same exception than before.
I suspect Bootstrapper is initializing the profiles in the wrong order, but I don't know how to solve it without abandoning Bootstrapper. Any suggestions or solutions to this problem?
Thanks