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)