Answer the question
In order to leave comments, you need to log in
How to display a list of all properties and everything related to them?
The task of reflection, I do not understand it at all. So the task is:
List all methods and their type and input parameters. Also output a list of all properties, their type, get set mark value.
At the same time, make an attribute, marking which in a property / method, they are not displayed. (As I understand it, this is a boolean field)
using System;
using System.Reflection;
namespace ConsoleApp3
{
class ReflectClass //тут пишутся методы для вывода
{
double d, f;
public ReflectClass(double d, double f)
{
this.d = d;
this.f = f;
}
public double MyTest(double d, double f)
{
return d + f;
}
public void Set(double d, double f)
{
}
public static void MethodReflectInfo<T>(T obj) where T: class //конкретно тут танцы с рефлексией
{
Type t = typeof(T);
MethodInfo[] MArr = t.GetMethods();
Console.WriteLine("Список методов класса {0}\n", obj.ToString());
foreach (MethodInfo m in MArr)
{
Console.Write("-->"+m.ReturnType.Name + " \t" + m.Name + "(");
ParameterInfo[] p = m.GetParameters();
for (int i = 0; i < p.Length; i++)
{
Console.Write(p[i].ParameterType.Name + " " + p[i].Name);
if (i + 1 < p.Length)
{
Console.Write(", ");
}
}
Console.Write(")\n");
}
}
}
class Program
{
static void Main(string[] args) //точка входа в программу
{
ReflectClass mts = new ReflectClass(12.0, 3.5); //вызов метода
ReflectClass.MethodReflectInfo<ReflectClass>(mts); //что то непонятное
Console.ReadLine();
}
}
}
Answer the question
In order to leave comments, you need to log in
Hello everyone, I came to the decision myself. I'm posting my own solution, in case it's useful to someone.
using System;
using System.Reflection;
using System.Linq;
namespace _123
{
[AttributeUsage((AttributeTargets.Method))] //атрибут на метод
public class HiddenMethod : System.Attribute
{
}
[AttributeUsage((AttributeTargets.Property))] //атрибут на свойство
public class HiddenProperty : System.Attribute
{
}//Вообще атрибуты сверху можно заменить одним,
//просто в качестве параметра AttributeUsage прописать
//(AttributeTargets.Method || AttributeTargets.Property)
public class ReflectClass //перечисление методов и свойств
{
double d, f;
[HiddenProperty]
public string DefaultMessage { get; set; } = "Hello world"; //Свойства
public string SecondMessage { get; set; } = "Hi";
public string ThirdMessage { get; set; } = "Bye";
public ReflectClass(double d, double f) //к конструктору нельзя применять атрибуты
{
this.d = d;
this.f = f;
}
public double MyTest(double d, double f) //Методы
{
return d + f;
}
[HiddenMethod]
public void Set1(double d, double f)
{
}
public void Set2(double d, double f)
{
}
public void Set3(double d, double f)
{
}
public static void MethodReflectInfo<T>(T obj) where T : class //основной класс
{
Type t = typeof(T);//Определение типа
Console.WriteLine("Список методов класса {0}\n", obj.ToString());
var meths = t.GetMethods().Where(p => !p.GetCustomAttributes<HiddenMethod>().Any() && !p.Name.Contains("get_") && !p.Name.Contains("set_")).ToList();
foreach (MethodInfo m in meths) //поиск методов
{
//if ((!(m.GetCustomAttribute<HiddenMethod>() == null)) && (!(m.Name.Contains("_get"))) && (!(m.Name.Contains("_set"))))//проверка
//{
Console.Write("-->" + m.ReturnType.Name + " \t" + m.Name + "(");
ParameterInfo[] p = m.GetParameters();
for (int i = 0; i < p.Length; i++)
{
Console.Write(p[i].ParameterType.Name + " " + p[i].Name);
if (i + 1 < p.Length)
{
Console.Write(", ");
}
}
Console.Write(")\n");
//}
}
Console.WriteLine("Список свойств класса {0}\n", obj.ToString());
var props = t.GetProperties().Where(p => !p.GetCustomAttributes<HiddenProperty>().Any()).ToList();
foreach (PropertyInfo m in props) //поиск свойств
{
//if (((m.GetCustomAttribute<HiddenProperty>() != null)) || (m.Name.Contains("_get") || (m.Name.Contains("_set"))))//проверка
//{
Console.Write("-->" + " \t" + m.Name + " \t" + " Can read:" + m.CanRead + " \t" + " Can write:" + m.CanWrite);
Console.Write("\n");
//}
}
}
}
class Program
{
static void Main(string[] args)
{
ReflectClass mts = new ReflectClass(12.0, 3.5);
ReflectClass.MethodReflectInfo<ReflectClass>(mts);
Console.ReadLine();
}
}
}
At the same time, make an attribute, marking which in a property / method, they are not displayed. (As I understand it, this is a boolean field)
var type = typeof(Test);
var members = type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
foreach (var member in members)
{
if (member.GetCustomAttribute<IgnoreItAttribute>() is null) // Проверяем, что атрибут IgnoreIt не стоит
{
var str = member switch
{
MethodInfo method => FormatMethod(method),
_ => null
};
if (str is not null)
Console.WriteLine(str);
}
}
static string FormatMethod(MethodInfo method) => method.ToString();
class IgnoreItAttribute : Attribute { }
class Test
{
public void Method1() { }
[IgnoreIt]
public int Method2(int a, int b) => 0;
}
their type and input parameters
Console.WriteLine(m.ToString());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question