Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Peck 690 posts 1896 karma points c-trib
    May 09, 2015 @ 16:50
    David Peck
    0

    Mapping delegate created from a generic type definition, doesn't work

    This is probably a little bit niche, and could just be my code rather than this awesome project, but I've found that if my mapping delegate returns a generic property then it doesn't work. E.g.

    public interface IMapClass{
            CustomMapping GetCustomMapping();
    }
    public class GenericTypeDefClass<T> : IMapClass where T is class, new(){
        public CustomMapping GetCustomMapping()
            {
                return MapProperty;
            }
    
            public static T MapProperty(IUmbracoMapper mapper, IPublishedContent contentToMapFrom, string propertyName, bool recursive)
            {
                return contentToMapFrom.GetPropertyValue<T>(propertyName, recursive, null);
            }
    }
    
    //Create a type for GenericTypeDefClass<string>
    Type newType = typeof(GenericTypeDefClass<T>).MakeGenericType(typeof(string));
    //Create an instance of GenericTypeDefClass<string>
    IMapClass c = (IMapClass)Activator.CreateInstance(type);
    //Get the delegate
    var mappingDelegate = c.GetCustomMapping();
    //arguments are typeof(string).FullName and the MapProperty delegate where T is string
    Mapper.AddCustomMapping(mappingDelegate.Method.ReturnType.FullName, mappingDelegate);

    I can imagine this isn't a problem that many people are coming across, but perhaps you can identify if there is a way around this issue?

    Of course, this is actually a moot point if my other post has a solution... 

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    May 10, 2015 @ 13:03
    Andy Butland
    0

    See how my suggestions go on the other post.  Hopefully they are easier ways built in - that might need an extra step, but simpler ones.  As if I'm honest, this approach with generics and reflection is getting a bit outside my comfort zone!

  • David Peck 690 posts 1896 karma points c-trib
    May 11, 2015 @ 15:10
    David Peck
    0

    No problem. Thanks for all your work in the project.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies