在分部类中创建静态方法 [英] Creating static method in partial class
问题描述
我需要在分部类中创建一个静态方法。
公共部分类Classname()
{
public bool HasProperty(此类型obj,字符串属性名称)
      {
          return obj.GetType()。GetProperty(propertyName)!= null;
      }
}
它的抛出错误如:
"扩展方法必须在非-generic static class"
如何解决?
提前致谢。
在"this"中使用"this"你的方法声明,你说这是一个扩展方法。
假设这是你的意图,扩展方法必须是静态的,并进入静态类。
public static TypeExtensions
{
public static bool HasProperty(this Type obj,string propertyName)
{
return obj.GetType()。GetProperty (propertyName)!= null;
}
}请参阅 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/延伸的方法
Hi,
I need to create a static method in partial class.
Public Partial class Classname()
{
public bool HasProperty(this Type obj, string propertyName)
{
return obj.GetType().GetProperty(propertyName) != null;
}
}
Its throwing error like :
"Extension method must be defined in a non-generic static class"
How to fix it?
Thanks in advance.
By using the word 'this' in your method declaration, you are saying that it is an extension method.
Assuming that is what you intended, extension methods must be static and go in static classes.
public static TypeExtensions { public static bool HasProperty(this Type obj, string propertyName) { return obj.GetType().GetProperty(propertyName) != null; } }
这篇关于在分部类中创建静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!