Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发射映射器。将对象转换为int_C#_.net_Emitmapper - Fatal编程技术网

C# 发射映射器。将对象转换为int

C# 发射映射器。将对象转换为int,c#,.net,emitmapper,C#,.net,Emitmapper,当我试图将对象映射到int时遇到一些问题 我的类和方法,其中转换: [Serializable] public class ProfileProperty { public object PropertyValue { get; set; } public bool IsVisible { get; set; } public ProfileProperty(object value, bool isVisible = true) { this


当我试图将
对象
映射到
int
时遇到一些问题
我的类和方法,其中转换:

[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;
    }

    public override string ToString()
    {
        if (this.PropertyValue != null)
        {
            return this.PropertyValue.ToString();
        }

        return string.Empty;
    }
}

[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;
    }

    public override string ToString()
    {
        if (this.PropertyValue != null)
        {
            return this.PropertyValue.ToString();
        }

        return string.Empty;
    }
}

public static class Helper
{
    public static ProfileModel PopulProfMod(Profile profile)
    {
        var mapper = ObjectMapperManager.DefaultInstance.GetMapper<Profile, ProfileModel>(new DefaultMapConfig()
                    .IgnoreMembers<Profile, ProfileModel>(new string[] { "GetValue", "ToString" }));
        ProfileModel prof = new ProfileModel();
        if (profile != null)
        {
            prof = mapper.Map(profile);
            //prof.Age = (int)profile.Age.PropertyValue;
            prof.Visibility = new List<string>();
        }

        //Some code

        return prof;
    }
}
您需要一个转换器:

  new DefaultMapConfig()
       .IgnoreMembers<Profile, ProfileModel>(new string[] { "GetValue", "ToString" }))
       .ConvertUsing<ProfileProperty, int>(s => (int)s.PropertyValue)
newdefaultmapconfig()
.IgnoreMembers(新字符串[]{“GetValue”,“ToString”}))
.ConvertUsing(s=>(int)s.PropertyValue)
  new DefaultMapConfig()
       .IgnoreMembers<Profile, ProfileModel>(new string[] { "GetValue", "ToString" }))
       .ConvertUsing<ProfileProperty, int>(s => (int)s.PropertyValue)