zoukankan      html  css  js  c++  java
  • C#判断一个类中有无"指定名称"的方法

    C#中可以通过反射分析元数据来解决这个问题,示例代码如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    using System;
    using System.Reflection;
     
    namespace Hello
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (IsMethodDefined(typeof(Utils), "HelloWorld"))
                {
                    Console.WriteLine("Utils类中有方法HelloWorld");
                }
                else
                {
                    Console.WriteLine("Utils类中没有方法HelloWorld");
                }
                Console.ReadKey();
            }    
            
            /// <summary>
            /// 判断一个类中有无"指定名称"的方法
            /// </summary>
            /// <param name="type"></param>
            /// <param name="methodName"></param>
            /// <returns></returns>
            static bool IsMethodDefined(Type type,string methodName)
            {
                bool result = false;
                foreach (MemberInfo m in type.GetMembers())
                {
                    if (m.Name == methodName)
                    {
                        result = true;
                        break;
                    }
                }
                return result;
            }
        }
     
        public static class Utils
        {
            public static void HelloWorld()
            {
                Console.WriteLine("Hello World!");
            }
        }
    }
  • 相关阅读:
    个人号微信机器人开发
    群控系统开发sdk服务端调用方法
    微信个人号scrm客服通信协议定义
    微信crm客服系统使用sdk定制开发(持续更新中!)
    微信客服crm系统接口定义(完善中)
    压测工具-ab
    设计模式之美学习-结构型-享元模式(二十五)
    设计模式之美学习-结构型-组合模式(二十四)
    设计模式之美学习-结构型-门面模式(二十三)
    设计模式之美学习-结构型-适配器模式(二十二)
  • 原文地址:https://www.cnblogs.com/chengjun/p/4169871.html
Copyright © 2011-2022 走看看