如何为许多类添加扩展方法?

Hou*_*han 25 c# reflection

我有大约1000个类,我需要计算它的属性数.我有以下代码:

    public static int NumberOfProperties()
    {
        Type type = typeof(C507);
        return type.GetProperties().Count();
    }
Run Code Online (Sandbox Code Playgroud)

我可以将其复制并粘贴到每个更改typeof参数的类中,但这看起来有点单调乏味.

反正有没有做一个扩展方法来做到这一点var nop = C507.NumberOfProperties();

Ren*_*ogt 43

只是添加建议object完整性扩展的答案:您还可以考虑仅针对Type以下内容实施扩展:

public static int GetPropertyCount(this Type t)
{
    return t.GetProperties().Length;
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

typeof(C507).GetPropertyCount();
Run Code Online (Sandbox Code Playgroud)

优点是您可以直接从类型中获取属性数,而不必先创建实例.

  • 当然,如果有上千个课程,你需要称它为1000次 (4认同)
  • @CallumLinington这是一个扩展方法,无需将其复制到所有类.你只需要实现一次.在接受的答案中,你也必须为每种类型调用它.`typeof(C507).GetPropertyCount()`对我来说似乎并不比`NumberOfProperties <C507>()`更详细.在这两种情况下,您必须为所有类型更改"C507". (2认同)

Cal*_*ton 29

因此,您可以编写使用对象或使用类型的扩展方法.

public static class ObjectExtensions
{
    public static int GetNumberOfProperties(this object value)
    {
        return value.GetType().GetProperties().Count();
    }

    public static int GetNumberOfProperties(this Type value)
    {
        return value.GetProperties().Count();
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

new C507().GetNumberOfProperties();
typeof(C507).GetNumberOfProperties();
Run Code Online (Sandbox Code Playgroud)

但是,您明确说明了两件事:

我可以将其复制并粘贴到每个更改typeof的类中

我有大约1000个班级

您可能不希望实例化1000个类或复制并粘贴typeof()1000次

在这种情况下,您将需要从程序集中读取它们.

所以类似于:

typeof(SomeClass).Assembly.GetTypes().Select(x => new
  {
       x.Name, 
       PropertyCount = x.GetType().GetProperties().Count()
  });
Run Code Online (Sandbox Code Playgroud)

哪里SomeClass是一个类(并不重要),所有的类所在.

我只是简单地将它们选择到一个包含Types名称和属性计数的匿名对象中.

这个:

typeof(SomeClass).Assembly
Run Code Online (Sandbox Code Playgroud)

只是一种方便的方式来获得组装.还有其他方法.

Assembly.GetAssembly(typeof(Program)).GetTypes()
Assembly.GetCallingAssembly().GetTypes()
Assembly.Load("Some Assemble Ref").GetTypes()
Run Code Online (Sandbox Code Playgroud)

你可以使用你找到的类型做各种各样的事情.如果选择Type自身,可以稍后使用Activator.CreateInstance(如果它具有无参数构造函数)对其进行实例化.您也可以使用反射自动填充属性.


Cod*_*dor 21

你想象的不可能有一个静态扩展方法.话虽这么说,可以在helper类中创建一个泛型方法,如下所示.

public static int NumberOfProperties<T>()
{
    Type type = typeof(T);
    return type.GetProperties().Count();
}
Run Code Online (Sandbox Code Playgroud)

给定一种类型,SomeType它可以被称为int n = NumberOfProperties<SomeType>().

  • Codor ......太棒了. (24认同)
  • 10th +1 Codor xD (4认同)
  • @Liam:虽然`object`上的扩展方法是完全可能的,但正如其他答案所暗示的那样,不可能将它设置为`static`,这样就可以进行类似`SomeType.NumberOfProperties()`的调用.为此,类必须从实现反射逻辑的相同基类型派生. (3认同)
  • 我有点困惑*没有你想象的静态扩展方法.*?其他答案似乎表明这是完全可能的.我误会了吗? (2认同)

Dav*_*idG 9

您可以object像这样制作扩展方法:

public static int PropertyCount(this object thing)
{
    return thing.GetType().GetProperties().Count();
}
Run Code Online (Sandbox Code Playgroud)

并在任何你喜欢的对象上使用它:

var x = "some string";
var numProps = x.PropertyCount();
Run Code Online (Sandbox Code Playgroud)


Dmi*_*nko 6

如果您想要一个扩展方法object:

public static ObjectExtensions
{
    public static int NumberOfProperties(this object value)
    {
        if (null == value)
          throw new ArgumentNullException("value"); // or return 0

        // Length: no need in Linq here
        return value.GetType().GetProperties().Length;
    }
}

...

C507 myObj = new C507();

// How many properties does myObj instance have?
int propCount = myObj.NumberOfProperties(); 
Run Code Online (Sandbox Code Playgroud)

如果你想要一个extesnion方法Type:

   public static TypeExtensions
    {
        public static int NumberOfProperties(this Type value)
        {
            if (null == value)
              throw new ArgumentNullException("value"); // or return 0

            // Length: no need in Linq here
            return value.GetProperties().Length;
        }
    }

    ...


    // How many properties does C507 type have?
    int propCount = typeof(C507).NumberOfProperties(); 
Run Code Online (Sandbox Code Playgroud)