Search code examples
c#.netmappingemitmapper

Emit mapper and Generic method



I have some problem with Emit mapper when I try to save in database properties.
In first I mapped this class (it work good):

[Serializable]
public class ProfileProperty 
{
    public string PropertyValue { get; set; }

    public bool IsVisible { get; set; }

    public ProfileProperty(string value, bool isVisible = true)
    {
        this.PropertyValue = value;
        this.IsVisible = isVisible;
    }

    public ProfileProperty()
    {
        this.IsVisible = true;
    }
}

I mapped here:

var mapper = ObjectMapperManager.DefaultInstance.GetMapper<PollmericaProfile, ProfileModel>();
ProfileModel prof = new ProfileModel();
if (x.User != null)
{
    prof = mapper.Map(x);
}

But some of the requirements need not a string property. That's why I decided to write this:

[Serializable]
public class ProfileProperty 
{
    public object PropertyValue { get; set; }

    public bool IsVisible { get; set; }

    public ProfileProperty(object value, bool isVisible = true)
    {
        this.PropertyValue = value;
        this.IsVisible = isVisible;
    }

    public ProfileProperty()
    {
        this.IsVisible = true;
    }

    public T GetValue<T>()
    {
        return (T)this.PropertyValue;
    }
}

And all mapping is not worked =(
If you ccan, help me please. If you want I can provide the necessary information.

P.S. To be honest, I want to transfer to a string and back, so at least works

UPD: I tried without method public T GetValue<T>()... It works...


Solution

  • Sorry for this, but I find answer very quicly.
    in mapping I must to write this:

    var mapper = ObjectMapperManager
                   .DefaultInstance
                   .GetMapper<PollmericaProfile, ProfileModel>( new DefaultMapConfig()
                   .IgnoreMembers<PollmericaProfile, ProfileModel>(new string[] { "GetValue" }));
    ProfileModel prof = new ProfileModel();
    if (x.User != null)
    {
        prof = mapper.Map(x);
    }