在分部类中创建静态方法 [英] Creating static method in partial class

查看:51
本文介绍了在分部类中创建静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要在分部类中创建一个静态方法。


公共部分类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;
        }
}

See https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods


这篇关于在分部类中创建静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆