Q&安培; A - 如何获得属性/功能/动作/(的接口或类)方法的名称,在一个强类型的方法吗? [英] Q&A - How to get the name of Property / Function / Action / Method (of an Interface or Class), in a strongly type way?

查看:167
本文介绍了Q&安培; A - 如何获得属性/功能/动作/(的接口或类)方法的名称,在一个强类型的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明

假设你有一个接口/类,并希望得到一个属性/功能/动作名称,如何和什么就是这样做的最好的做法是什么?

Assuming you have an interface / class, and wish to get a Property/Func/Action name, how and what is the best practice to do so?

如给出:

public interface IConvertible
{    
    // ...
    bool ToBoolean(IFormatProvider provider);
    // ...
}



如何获得的名ToBoolean的方法,在一个强类型的方式?

How to get the name of 'ToBoolean' method, in a strongly-typed way?

另外,如何为获得属性名称 IsValueCreated从

Also, how to get the property name of IsValueCreated from

Lazy<object>.IsValueCreated

激励

当你将一个接口[方法/属性/等]在做反思,编译器会帮你找到所有,它引用

When you'll do reflection on an interface [method / property / etc], the compiler will help you find all references to it.

推荐答案

对于.NET 5及以上版本:
使用<一个HREF =htt​​ps://msdn.microsoft.com/en-us/library/dn986596.aspx相对=nofollow> nameof (学分的 M-kazem-akhgary

对于.NET 4.5V及以下:

using System;
using System.Linq.Expressions;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using LazyHelper = DotNetSandbox.InterfaceNames<System.Lazy<object>>;

namespace DotNetSandbox
{
    // Use case / Example:
    [TestClass]
    public class InterfaceNamesTests
    {
        [TestMethod]
        public void InterfaceNamesTest()
        {
            // Option 1 - Not strongly typed
            string nameA = typeof(IConvertible).GetMethod("ToBoolean").Name;
            Console.WriteLine(nameA);    // OUTPUT: ToBoolean
            string nameB = typeof(Lazy<object>).GetProperty("IsValueCreated").Name;
            Console.WriteLine(nameB);    // OUTPUT: ToBoolean

            // *** Option 2 ***************
            Console.WriteLine();
            Console.WriteLine("Option 2 - Strongly typed way:");
            IConvertible @interface = InterfaceNames<IConvertible>.Create();

            Func<IFormatProvider, bool> func = @interface.ToBoolean;
            string name1 = func.Method.Name;
            Console.WriteLine(name1);   // OUTPUT: ToBoolean

            string propName = InterfaceNames<Lazy<object>>.GetPropertyName(i => i.IsValueCreated);
            Console.WriteLine(propName);   // OUTPUT: IsValueCreated
            //OR (For short version - declare alias using, see using region..)
            string propName2 = LazyHelper.GetPropertyName(i => i.IsValueCreated);
            Console.WriteLine(propName2);   // OUTPUT: IsValueCreated


            // Other options results with complex/unclear code.
        }
    }

    // Helper class
    public class InterfaceNames<T> : RealProxy
    {
        private InterfaceNames() : base(typeof(T)) { }

        public static T Create()
        {
            return (T)new InterfaceNames<T>().GetTransparentProxy();
        }

        public override IMessage Invoke(IMessage msg)
        {
            return null;
        }

        public static string GetName(Delegate d)
        {
            return d.Method.Name;
        }

        public static string GetPropertyName<TProp>(Expression<Func<T, TProp>> prop)
        {
            // Credits to p-s-w-g
            var body = prop.Body as MemberExpression;
            return body == null ? null : body.Member.Name;
        }
    }
}



资源

  • Using obect's property names in strongly type way
  • C# Strongly Typed Attribute member to describe property
  • Get the name(s) of interface methods strong typed

等待您的输入/建议/见解...

Waiting for your input / suggestions / insights...

这篇关于Q&安培; A - 如何获得属性/功能/动作/(的接口或类)方法的名称,在一个强类型的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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