Home  >  Article  >  Backend Development  >  How to expose methods and properties using reflection in C#?

How to expose methods and properties using reflection in C#?

王林
王林forward
2023-08-28 19:01:051069browse

如何在 C# 中使用反射来显示方法和属性?

Reflection is the process of describing metadata for types, methods, and fields in code. The System.Reflection namespace enables you to obtain data about a loaded assembly and its elements such as classes, methods, and value types. There are many classes of System.Reflection, but the most commonly used ones are Assembly, AssemblyName, ConstructorInfo, MethodInfo, ParameterInfo, EventInfo, PropertyInfo and MemberInfo.

Example

static void Main(string[] args){
   TypeInfo myType = typeof(TextInfo).GetTypeInfo();
   IEnumerable<PropertyInfo> properties = myType.DeclaredProperties;
   IEnumerable<MethodInfo> methods = myType.DeclaredMethods;
   Console.WriteLine(myType);
   Console.WriteLine(properties);
   Console.WriteLine(methods);
   StringBuilder strBuilder = new StringBuilder();
   Console.WriteLine();
   strBuilder.Append("The properties are:");
   foreach (PropertyInfo p in properties){
      strBuilder.Append("" + p.Name);
   }
   strBuilder.Append("");
   strBuilder.Append("The methods are:");
   foreach (MethodInfo m in methods){
      strBuilder.Append("" + m.Name);
   }
   Console.WriteLine(strBuilder);
}

Output

System.Globalization.TextInfo
System.Reflection.PropertyInfo[]
System.Reflection.MethodInfo[]
The properties are:
Invariant
ANSICodePage
OEMCodePage
MacCodePage
EBCDICCodePage
LCID
CultureName
IsReadOnly
ListSeparator
IsAsciiCasingSameAsInvariant
IsRightToLeft
The methods are:
get_Invariant
get_ANSICodePage
get_OEMCodePage
get_MacCodePage
get_EBCDICCodePage
get_LCID
get_CultureName
get_IsReadOnly
get_ListSeparator
set_ListSeparator
get_IsAsciiCasingSameAsInvariant
get_IsRightToLeft
System.Runtime.Serialization.IDeserializationCallback.OnDeserialization
Clone
ReadOnly
VerifyWritable
SetReadOnlyState
ToLower
ToLower
ChangeCase
ChangeCaseToLower
ChangeCaseToUpper
ChangeCaseCommon
ChangeCaseCommon
ChangeCaseCommon
ToLowerAsciiInvariant
ToLowerAsciiInvariant
ToUpperAsciiInvariant
ToUpperAsciiInvariant
ToLowerAsciiInvariant
ToUpper
ToUpper
ToUpperAsciiInvariant
IsAscii
PopulateIsAsciiCasingSameAsInvariant
Equals
GetHashCode
ToString
ToTitleCase
AddNonLetter
AddTitlecaseLetter
IsWordSeparator
IsLetterCategory
FinishInitialization
ChangeCase
IsInvariantLocale

The above is the detailed content of How to expose methods and properties using reflection in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete