zoukankan      html  css  js  c++  java
  • 关于C#中”扩展方法必须在非泛型静态类中定义“问题的解决

    问题描述:

           在某个窗口下的编码中使用了以下扩展方法FindControl,以求根据字符串的值去操作控件(本文中的控件为Label控件)的属性。

         public static Control FindControl(this Control parentControl, string findCtrlName)

            {

                Control _findedControl = null;

                if (!string.IsNullOrEmpty(findCtrlName) && parentControl != null)

                {

                    foreach (Control ctrl in parentControl.Controls)

                    {

                        if (ctrl.Name.Equals(findCtrlName))

                        {

                            _findedControl = ctrl;

                            break;

                        }

                    }

                }

                return _findedControl;

            }

           使用后错误列表中显示错误,错误描述为”扩展方法必须在非泛型静态类中定义“,错误的指示下划线指到了代码段中的Form2处。

    解决方案:

          在相同的名称空间下新建了一个静态类,类名为ExtensionClass,然后把扩展方法代码移至该类中。如下图:

     

          然后在Form2的代码块中通过引用该方法,实现了目的,如下图:

  • 相关阅读:
    [LeetCode] 222. Count Complete Tree Nodes Java
    [LeetCode] 199. Binary Tree Right Side View Java
    自动加载的两种办法
    Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted
    Java 面试
    Java 获取最近时间
    Java 倒序输出
    java 排序算法
    图片预览
    SSH三大框架的工作原理
  • 原文地址:https://www.cnblogs.com/xinweichen/p/6594262.html
Copyright © 2011-2022 走看看