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

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

王林
Release: 2023-08-28 19:01:05
forward
1116 people have browsed it

如何在 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);
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template