首页 > 解决方案 > 是否可以在 C# 中获取属性的所有引用?

问题描述

我想编写一个脚本来显示类属性的所有引用(类似于在 Visual Studio 中按 F12)。

它应该在 powershell 中运行或作为 C# .net 控制台应用程序运行。

该脚本的主要任务是删除 DataContext 中未使用的 DataModel。

非常感谢!

标签: c#asp.net.netvisual-studioreflection

解决方案


似乎您想使用反射来获取类的引用。

我在控制台应用程序中制作了一个代码示例。您可以检查它是否对您有用。

class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            Type type = stu.GetType();
            var members = type.GetMembers().ToList();
            foreach (var item in members)
            {
                Console.WriteLine(item);
            }

        }
    }


    public class Student
    { 
        public string Name { get; set; }

        public int Age { get; set; }

        public void SayHello(string Name)
        {
            Console.WriteLine("Hello , I am {0}",Name);
        }

        public int ClassID { get; set; }



    }

结果:

在此处输入图像描述


推荐阅读