我有大约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)
优点是您可以直接从类型中获取属性数,而不必先创建实例.
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>()
.
您可以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)
如果您想要一个扩展方法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)