C# 创建我自己的映射器

C# 创建我自己的映射器,c#,generics,mapping,ienumerable,C#,Generics,Mapping,Ienumerable,这就是我的情况。我有两个班,TmdbTvShow和TvShow TmdbTvShow是一个充满了我从外部源获得的数据的类。现在我想创建一个映射器,将它映射到我自己的类TvShow 班级电视节目: [MovieMap("TmdbTvShow")] public class TvShow { public int ID { get; set; } [MovieMapProperty("ID")] public int TmdbID { get; set; } [M

这就是我的情况。我有两个班,TmdbTvShow和TvShow

TmdbTvShow是一个充满了我从外部源获得的数据的类。现在我想创建一个映射器,将它映射到我自己的类TvShow

班级电视节目:

 [MovieMap("TmdbTvShow")]
public class TvShow
{
    public int ID { get; set; }
    [MovieMapProperty("ID")]
    public int TmdbID { get; set; }

    [MovieMapProperty("Name")]
    public string Name { get; set; }

    [MovieMapProperty("OriginalName")]
    public string OriginalName { get; set; }

    [MovieMapProperty("Overview")]
    public string Summary { get; set; }

    [MovieMapProperty("FirstAirDate")]
    public DateTime FirstAirDate { get; set; }

    [MovieMapProperty("LastAirDate")]
    public DateTime LastAirDate { get; set; }

    [MovieMapProperty("Genres")]
    public IEnumerable<Genre> Genres { get; set; }

    [MovieMapProperty("InProduction")]
    public bool Running { get; set; }

}
也许有人能帮我找到正确的方向来处理这件事


Cheers

您可以创建一个列表,它是一个
IEnumerable
,将项目添加到列表中,然后将其分配给属性

如果需要将对象从
TmdbGenre
转换为
Genre
,可以递归调用
Map
,但可能需要接受类型参数的
Map
版本

public object Map(Type type, object input);

var result = value.Select(x => Map(prop.GetType().GetGenericArguments()[0], x));
prop.SetValue(obj, result);

好吧,经过很多尝试,我终于明白了

以下是我的解决方案:

 public T Map<T>(object input) where T : new()
    {
        T obj = new T();

        MovieMapAttribute[] classAttributes = (MovieMapAttribute[])obj.GetType().GetCustomAttributes(typeof(MovieMapAttribute), false);

        if (classAttributes != null && classAttributes[0].ClassName.Equals(input.GetType().Name))
        {
            Dictionary<string, MovieMapPropertyAttribute> propAtts = new Dictionary<string, MovieMapPropertyAttribute>();

            foreach (PropertyInfo prop in obj.GetType().GetProperties())
            {
                MovieMapPropertyAttribute[] mma = (MovieMapPropertyAttribute[])prop.GetCustomAttributes(typeof(MovieMapPropertyAttribute), false);

                // Attribute found
                if (mma.Length > 0)
                {
                    // Get attribute
                    MovieMapPropertyAttribute mmp = mma[0];

                    if (input.GetType().GetProperty(mmp.PropertyName) != null)
                    {
                        // Get value
                        var value = input.GetType().GetProperty(mmp.PropertyName).GetValue(input, null);

                        // Is property a dateTime
                        if (typeof(DateTime).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, Convert.ToDateTime(value), null);
                        }
                        else if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string) && prop.PropertyType != typeof(string[]))
                        {
                            Type type = prop.PropertyType.GetGenericArguments()[0];
                            var list = (IEnumerable)value;
                            dynamic values = Activator.CreateInstance(typeof(List<>).MakeGenericType(type));

                            foreach (object ob in list)
                            {

                                object tempObj = Activator.CreateInstance(type);

                                tempObj = Map(ob, tempObj);

                                values.Add((dynamic)tempObj);
                            }

                            obj.GetType().GetProperty(prop.Name).SetValue(obj, values, null);
                        }
                        else if (typeof(Boolean).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, Convert.ToBoolean(value), null);
                        }
                        else if (typeof(int).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, Convert.ToInt32(value), null);
                        }
                        else if (typeof(float).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, float.Parse(value.ToString(), System.Globalization.CultureInfo.InvariantCulture.NumberFormat), null);
                        }
                        else
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, value, null);
                        }
                    }
                }        
            }

        }
        else
            throw new Exception("Wrong object");

        return obj;
    }       

    private object Map(object input, object output)
    {
        MovieMapAttribute[] classAttributes = (MovieMapAttribute[])output.GetType().GetCustomAttributes(typeof(MovieMapAttribute), false);

        if (classAttributes != null && classAttributes[0].ClassName.Equals(input.GetType().Name))
        {
            foreach (PropertyInfo prop in output.GetType().GetProperties())
            {
                MovieMapPropertyAttribute[] mma = (MovieMapPropertyAttribute[])prop.GetCustomAttributes(typeof(MovieMapPropertyAttribute), false);

                // Attribute found
                if (mma.Length > 0)
                {
                    // Get attribute
                    MovieMapPropertyAttribute mmp = mma[0];

                    if (input.GetType().GetProperty(mmp.PropertyName) != null)
                    {

                        var value = input.GetType().GetProperty(mmp.PropertyName).GetValue(input, null);

                        // Is property a dateTime
                        if (typeof(DateTime).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, Convert.ToDateTime(value), null);
                        }
                        else if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string) && prop.PropertyType != typeof(string[]))
                        {
                            Type type = prop.PropertyType.GetGenericArguments()[0];
                            var list = (IEnumerable)value;
                            dynamic values = Activator.CreateInstance(typeof(List<>).MakeGenericType(type));

                            foreach (object ob in list)
                            {

                                object tempObj = Activator.CreateInstance(type);

                                tempObj = Map(ob, tempObj);

                                values.Add((dynamic)tempObj);
                            }

                            output.GetType().GetProperty(prop.Name).SetValue(output, values, null);
                        }
                        else if (typeof(Boolean).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, Convert.ToBoolean(value), null);
                        }
                        else if (typeof(int).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, Convert.ToInt32(value), null);
                        }
                        else if (typeof(float).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, float.Parse(value.ToString(), System.Globalization.CultureInfo.InvariantCulture.NumberFormat), null);
                        }
                        else
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, value, null);
                        }
                    }                        
                }
            }
        }
        else
            throw new Exception("Wrong object");

        return output;
    }       
publictmap(对象输入),其中T:new()
{
T obj=新的T();
MovieMapAttribute[]classAttributes=(MovieMapAttribute[])obj.GetType().GetCustomAttributes(typeof(MovieMapAttribute),false);
if(classAttributes!=null&&classAttributes[0].ClassName.Equals(input.GetType().Name))
{
Dictionary propAtts=新字典();
foreach(obj.GetType().GetProperties()中的PropertyInfo属性)
{
MovieMapPropertyAttribute[]mma=(MovieMapPropertyAttribute[])prop.GetCustomAttributes(typeof(MovieMapPropertyAttribute),false);
//找到属性
如果(mma.Length>0)
{
//获取属性
MovieMapPropertyAttribute mmp=mma[0];
if(input.GetType().GetProperty(mmp.PropertyName)!=null)
{
//获得价值
var value=input.GetType().GetProperty(mmp.PropertyName).GetValue(input,null);
//属性是日期时间吗
if(typeof(DateTime).IsAssignableFrom(prop.PropertyType))
{
//将值设置为对象
obj.GetType().GetProperty(prop.Name).SetValue(obj,Convert.ToDateTime(value),null);
}
else if(typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)&&prop.PropertyType!=typeof(string)&&prop.PropertyType!=typeof(string[]))
{
Type Type=prop.PropertyType.GetGenericArguments()[0];
变量列表=(IEnumerable)值;
动态值=Activator.CreateInstance(typeof(List).MakeGenericType(type));
foreach(列表中的对象对象对象)
{
对象tempObj=Activator.CreateInstance(类型);
tempObj=Map(ob,tempObj);
添加((动态)tempObj);
}
obj.GetType().GetProperty(prop.Name).SetValue(obj,value,null);
}
else if(typeof(Boolean).IsAssignableFrom(prop.PropertyType))
{
//将值设置为对象
obj.GetType().GetProperty(prop.Name).SetValue(obj,Convert.ToBoolean(value),null);
}
else if(typeof(int).IsAssignableFrom(prop.PropertyType))
{
//将值设置为对象
obj.GetType().GetProperty(prop.Name).SetValue(obj,Convert.ToInt32(value),null);
}
else if(typeof(float).IsAssignableFrom(prop.PropertyType))
{
//将值设置为对象
obj.GetType().GetProperty(prop.Name).SetValue(obj,float.Parse(value.ToString(),System.Globalization.CultureInfo.InvariantCulture.NumberFormat),null);
}
其他的
{
//将值设置为对象
obj.GetType().GetProperty(prop.Name).SetValue(obj,value,null);
}
}
}        
}
}
其他的
抛出新异常(“错误对象”);
返回obj;
}       
私有对象映射(对象输入、对象输出)
{
MovieMapAttribute[]classAttributes=(MovieMapAttribute[])output.GetType().GetCustomAttributes(typeof(MovieMapAttribute),false);
if(classAttributes!=null&&classAttributes[0].ClassName.Equals(input.GetType().Name))
{
foreach(output.GetType().GetProperties()中的PropertyInfo属性)
{
MovieMapPropertyAttribute[]mma=(MovieMapPropertyAttribute[])prop.GetCustomAttributes(typeof(MovieMapPropertyAttribute),false);
//找到属性
如果(mma.Length>0)
{
//获取属性
MovieMapPropertyAttribute mmp=mma[0];
if(input.GetType().GetProperty(mmp.PropertyName)!=null)
{
var value=input.GetType().GetProperty(mmp.PropertyName).GetValue(input,null);
//属性是日期时间吗
if(typeof(DateTime).IsAssignableFrom(prop.PropertyType))
{
//将值设置为对象
output.GetType().GetProperty(prop.Name).SetValue(output,Convert.ToDateTime(value),null);
}
else if(typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)&&prop.PropertyType!=typeof(string)&&prop.PropertyType!=typeof(string[]))
{
Type Type=prop.PropertyType.GetGenericArguments()[0];
public object Map(Type type, object input);

var result = value.Select(x => Map(prop.GetType().GetGenericArguments()[0], x));
prop.SetValue(obj, result);
 public T Map<T>(object input) where T : new()
    {
        T obj = new T();

        MovieMapAttribute[] classAttributes = (MovieMapAttribute[])obj.GetType().GetCustomAttributes(typeof(MovieMapAttribute), false);

        if (classAttributes != null && classAttributes[0].ClassName.Equals(input.GetType().Name))
        {
            Dictionary<string, MovieMapPropertyAttribute> propAtts = new Dictionary<string, MovieMapPropertyAttribute>();

            foreach (PropertyInfo prop in obj.GetType().GetProperties())
            {
                MovieMapPropertyAttribute[] mma = (MovieMapPropertyAttribute[])prop.GetCustomAttributes(typeof(MovieMapPropertyAttribute), false);

                // Attribute found
                if (mma.Length > 0)
                {
                    // Get attribute
                    MovieMapPropertyAttribute mmp = mma[0];

                    if (input.GetType().GetProperty(mmp.PropertyName) != null)
                    {
                        // Get value
                        var value = input.GetType().GetProperty(mmp.PropertyName).GetValue(input, null);

                        // Is property a dateTime
                        if (typeof(DateTime).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, Convert.ToDateTime(value), null);
                        }
                        else if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string) && prop.PropertyType != typeof(string[]))
                        {
                            Type type = prop.PropertyType.GetGenericArguments()[0];
                            var list = (IEnumerable)value;
                            dynamic values = Activator.CreateInstance(typeof(List<>).MakeGenericType(type));

                            foreach (object ob in list)
                            {

                                object tempObj = Activator.CreateInstance(type);

                                tempObj = Map(ob, tempObj);

                                values.Add((dynamic)tempObj);
                            }

                            obj.GetType().GetProperty(prop.Name).SetValue(obj, values, null);
                        }
                        else if (typeof(Boolean).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, Convert.ToBoolean(value), null);
                        }
                        else if (typeof(int).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, Convert.ToInt32(value), null);
                        }
                        else if (typeof(float).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, float.Parse(value.ToString(), System.Globalization.CultureInfo.InvariantCulture.NumberFormat), null);
                        }
                        else
                        {
                            // Set value to object
                            obj.GetType().GetProperty(prop.Name).SetValue(obj, value, null);
                        }
                    }
                }        
            }

        }
        else
            throw new Exception("Wrong object");

        return obj;
    }       

    private object Map(object input, object output)
    {
        MovieMapAttribute[] classAttributes = (MovieMapAttribute[])output.GetType().GetCustomAttributes(typeof(MovieMapAttribute), false);

        if (classAttributes != null && classAttributes[0].ClassName.Equals(input.GetType().Name))
        {
            foreach (PropertyInfo prop in output.GetType().GetProperties())
            {
                MovieMapPropertyAttribute[] mma = (MovieMapPropertyAttribute[])prop.GetCustomAttributes(typeof(MovieMapPropertyAttribute), false);

                // Attribute found
                if (mma.Length > 0)
                {
                    // Get attribute
                    MovieMapPropertyAttribute mmp = mma[0];

                    if (input.GetType().GetProperty(mmp.PropertyName) != null)
                    {

                        var value = input.GetType().GetProperty(mmp.PropertyName).GetValue(input, null);

                        // Is property a dateTime
                        if (typeof(DateTime).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, Convert.ToDateTime(value), null);
                        }
                        else if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string) && prop.PropertyType != typeof(string[]))
                        {
                            Type type = prop.PropertyType.GetGenericArguments()[0];
                            var list = (IEnumerable)value;
                            dynamic values = Activator.CreateInstance(typeof(List<>).MakeGenericType(type));

                            foreach (object ob in list)
                            {

                                object tempObj = Activator.CreateInstance(type);

                                tempObj = Map(ob, tempObj);

                                values.Add((dynamic)tempObj);
                            }

                            output.GetType().GetProperty(prop.Name).SetValue(output, values, null);
                        }
                        else if (typeof(Boolean).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, Convert.ToBoolean(value), null);
                        }
                        else if (typeof(int).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, Convert.ToInt32(value), null);
                        }
                        else if (typeof(float).IsAssignableFrom(prop.PropertyType))
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, float.Parse(value.ToString(), System.Globalization.CultureInfo.InvariantCulture.NumberFormat), null);
                        }
                        else
                        {
                            // Set value to object
                            output.GetType().GetProperty(prop.Name).SetValue(output, value, null);
                        }
                    }                        
                }
            }
        }
        else
            throw new Exception("Wrong object");

        return output;
    }