简体   繁体   中英

Is it possible to get all references of a property in C#?

I want to write a script which shows all references on a property of a class (similar to pressing F12 in Visual Studio).

It should run in powershell or as a C# .net console application.

The main task of the script is to delete unused DataModels of a DataContext.

Thank you very much!

It seems that you want to use the reflection to get the references of a class.

I make a code example in the console app. You can check if it is useful for you.

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; }



    }

Result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM