Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.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# C获取C中某一类型对象的所有属性_C# - Fatal编程技术网

C# C获取C中某一类型对象的所有属性

C# C获取C中某一类型对象的所有属性,c#,C#,我有一个有10个文件的对象。我想把它们放在一个文件列表中,这样就可以很容易地进行操作 public class Car{ public IFormFile Imagem1 {get; set;} public IFormFile Imagem2 {get; set;} ...... } List<IFormFile> imagens = new List<IFormFile>(); foreach(IFormFile imagem in carro.)

我有一个有10个文件的对象。我想把它们放在一个文件列表中,这样就可以很容易地进行操作

public class Car{

 public IFormFile Imagem1 {get; set;}
 public IFormFile Imagem2 {get; set;}
......

}



 List<IFormFile> imagens = new List<IFormFile>();

foreach(IFormFile imagem in carro.)
  {
          imagens.add(...);
  }
是否有方法将文件传递给lista,或者我必须在Car对象中使用它们

我忘了说我是用

images.addcar.Image1


但是我的代码越来越乱了。因为我必须检查空值和其他很多东西。如果我能在一个循环中获得ifformfile,我的生活就会轻松得多。

下面的代码演示了如何从对象car获取ifformfile类型的所有属性

正如在评论中提到的,反射API是相当慢的——考虑缓存属性信息对象,或者使用表达式编译委托,更好地迭代对象属性并将它们的值放到目标集合中。

void Main()
{
    var car = new Car();

    var imagens = typeof(Car).GetProperties()
        .Where(x => x.PropertyType == typeof(IFormFile))
        .Select(x => (IFormFile)x.GetValue(car))
        .Where(x => x != null)
        .ToList();
}
属性信息缓存 下面是如何将上面的代码转换为使用缓存的PropertyInfo对象的示例:


下面的代码演示了如何从对象car获取ifformfile类型的所有属性

正如在评论中提到的,反射API是相当慢的——考虑缓存属性信息对象,或者使用表达式编译委托,更好地迭代对象属性并将它们的值放到目标集合中。

void Main()
{
    var car = new Car();

    var imagens = typeof(Car).GetProperties()
        .Where(x => x.PropertyType == typeof(IFormFile))
        .Select(x => (IFormFile)x.GetValue(car))
        .Where(x => x != null)
        .ToList();
}
属性信息缓存 下面是如何将上面的代码转换为使用缓存的PropertyInfo对象的示例:


下面是一个从提供的对象返回指定类型的所有属性的方法:

public static List<TProperty> GetAllPropertyValuesOfType<TProperty>(this object obj)
{
    return obj.GetType()
        .GetProperties()
        .Where(prop => prop.PropertyType == typeof(TProperty))
        .Select(pi => (TProperty)pi.GetValue(obj))
        .ToList();
}
您可以这样使用它:

Car myCar;
List<IFormFile> imagesOfMyCar = myCar.GetAllPropertyValuesOfType<IFormFile>();

下面是一个从提供的对象返回指定类型的所有属性的方法:

public static List<TProperty> GetAllPropertyValuesOfType<TProperty>(this object obj)
{
    return obj.GetType()
        .GetProperties()
        .Where(prop => prop.PropertyType == typeof(TProperty))
        .Select(pi => (TProperty)pi.GetValue(obj))
        .ToList();
}
您可以这样使用它:

Car myCar;
List<IFormFile> imagesOfMyCar = myCar.GetAllPropertyValuesOfType<IFormFile>();

你可以使用反射。GetProperties方法返回特定类型的所有属性

例如,您的代码必须如下所示:

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    public class Car
    {

        public IFormFile Imagem1 { get; set; }
        public IFormFile Imagem2 { get; set; }
        public IFormFile Imagem3 { get; set; }
        //and etc
    }

    public class Example
    {
        public static void Main()
        {
            List<IFormFile> imagens = new List<IFormFile>();

            var car = new Car();

            var carType = car.GetType();
            var ifromFileProperties = carType.GetProperties().Where(x => x.PropertyType == typeof(IFormFile)).ToArray();

            foreach (var property in ifromFileProperties)
            {
                var image = (IFormFile)property.GetValue(car, null);
                imagens.Add(image);
            }
        }
    }
}

最后,列表中有所有属性类型为文件的项目

您可以使用反射。GetProperties方法返回特定类型的所有属性

例如,您的代码必须如下所示:

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    public class Car
    {

        public IFormFile Imagem1 { get; set; }
        public IFormFile Imagem2 { get; set; }
        public IFormFile Imagem3 { get; set; }
        //and etc
    }

    public class Example
    {
        public static void Main()
        {
            List<IFormFile> imagens = new List<IFormFile>();

            var car = new Car();

            var carType = car.GetType();
            var ifromFileProperties = carType.GetProperties().Where(x => x.PropertyType == typeof(IFormFile)).ToArray();

            foreach (var property in ifromFileProperties)
            {
                var image = (IFormFile)property.GetValue(car, null);
                imagens.Add(image);
            }
        }
    }
}

最后,列表中有所有属性类型为ifformfile的项

如果它们总是为10,则var imagens=new[]{car.Imagem1,car.Imagem2,…};比使用Reflection性能更好为什么不将它们存储为汽车类中的列表?如果你需要一个标识符,那么你可以使用字典。如果它总是十个,答案是:是,不是。对象将始终具有10个文件,但它们可以为null或其他一些无效值。这是因为我想使用一个循环,以便进行验证。@jason.kaisersmith它们是映射到用户请求的上传图像。如果我改变它,我必须至少改变前端。如果它们总是10,那么var imagens=new[]{car.Imagem1,car.Imagem2,…};比使用Reflection性能更好为什么不将它们存储为汽车类中的列表?如果你需要一个标识符,那么你可以使用字典。如果它总是十个,答案是:是,不是。对象将始终具有10个文件,但它们可以为null或其他一些无效值。这是因为我想使用一个循环,以便进行验证。@jason.kaisersmith它们是映射到用户请求的上传图像。如果我改变它,我将不得不改变前端至少。