C# 如何在运行时获取静态类的方法名?
如何在像C# 如何在运行时获取静态类的方法名?,c#,.net,vb.net,C#,.net,Vb.net,如何在像Math这样的静态类中获取所有方法的名称 输出应类似于以下内容: Sin Cos Round ... 试试这个: MethodInfo[] methodInfos = typeof(Math).GetMethods(BindingFlags.Public | BindingFlags.Static); foreach (MethodInfo methodInfo in methodInfos) { Console.WriteLine(method
Math
这样的静态类中获取所有方法的名称
输出应类似于以下内容:
Sin
Cos
Round
...
试试这个:
MethodInfo[] methodInfos = typeof(Math).GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (MethodInfo methodInfo in methodInfos)
{
Console.WriteLine(methodInfo.Name);
}
另外,如果您还需要知道所有参数,请尝试以下方法
private static void Main()
{
MethodInfo[] methodInfos = typeof(Math).GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (MethodInfo methodInfo in methodInfos)
{
Console.WriteLine(String.Format("{0} with following parameters", methodInfo.Name));
ParameterInfo[] parameters = methodInfo.GetParameters();
foreach (ParameterInfo parameter in parameters)
{
Console.WriteLine("Name : {0}, Type : {1}", parameter.Name, parameter.ParameterType.FullName);
}
Console.WriteLine("--------------");
}
}
输出:
Acos with following parameters
Name : d, Type : System.Double
--------------
Asin with following parameters
Name : d, Type : System.Double
--------------
Atan with following parameters
Name : d, Type : System.Double
--------------
Atan2 with following parameters
Name : y, Type : System.Double
Name : x, Type : System.Double
--------------
Ceiling with following parameters
Name : d, Type : System.Decimal
--------------
Ceiling with following parameters
Name : a, Type : System.Double
--------------
Cos with following parameters
Name : d, Type : System.Double
--------------
Cosh with following parameters
Name : value, Type : System.Double
--------------
Floor with following parameters
Name : d, Type : System.Decimal
--------------
Floor with following parameters
Name : d, Type : System.Double
--------------
Sin with following parameters
Name : a, Type : System.Double
--------------
Tan with following parameters
Name : a, Type : System.Double
--------------
Sinh with following parameters
Name : value, Type : System.Double
--------------
Tanh with following parameters
Name : value, Type : System.Double
--------------
Round with following parameters
Name : a, Type : System.Double
--------------
Round with following parameters
Name : value, Type : System.Double
Name : digits, Type : System.Int32
--------------
Round with following parameters
Name : value, Type : System.Double
Name : mode, Type : System.MidpointRounding
--------------
Round with following parameters
Name : value, Type : System.Double
Name : digits, Type : System.Int32
Name : mode, Type : System.MidpointRounding
--------------
Round with following parameters
Name : d, Type : System.Decimal
--------------
Round with following parameters
Name : d, Type : System.Decimal
Name : decimals, Type : System.Int32
--------------
Round with following parameters
Name : d, Type : System.Decimal
Name : mode, Type : System.MidpointRounding
--------------
Round with following parameters
Name : d, Type : System.Decimal
Name : decimals, Type : System.Int32
Name : mode, Type : System.MidpointRounding
--------------
Truncate with following parameters
Name : d, Type : System.Decimal
--------------
Truncate with following parameters
Name : d, Type : System.Double
--------------
Sqrt with following parameters
Name : d, Type : System.Double
--------------
Log with following parameters
Name : d, Type : System.Double
--------------
Log10 with following parameters
Name : d, Type : System.Double
--------------
Exp with following parameters
Name : d, Type : System.Double
--------------
Pow with following parameters
Name : x, Type : System.Double
Name : y, Type : System.Double
--------------
IEEERemainder with following parameters
Name : x, Type : System.Double
Name : y, Type : System.Double
--------------
Abs with following parameters
Name : value, Type : System.SByte
--------------
Abs with following parameters
Name : value, Type : System.Int16
--------------
Abs with following parameters
Name : value, Type : System.Int32
--------------
Abs with following parameters
Name : value, Type : System.Int64
--------------
Abs with following parameters
Name : value, Type : System.Single
--------------
Abs with following parameters
Name : value, Type : System.Double
--------------
Abs with following parameters
Name : value, Type : System.Decimal
--------------
Max with following parameters
Name : val1, Type : System.SByte
Name : val2, Type : System.SByte
--------------
Max with following parameters
Name : val1, Type : System.Byte
Name : val2, Type : System.Byte
--------------
Max with following parameters
Name : val1, Type : System.Int16
Name : val2, Type : System.Int16
--------------
Max with following parameters
Name : val1, Type : System.UInt16
Name : val2, Type : System.UInt16
--------------
Max with following parameters
Name : val1, Type : System.Int32
Name : val2, Type : System.Int32
--------------
Max with following parameters
Name : val1, Type : System.UInt32
Name : val2, Type : System.UInt32
--------------
Max with following parameters
Name : val1, Type : System.Int64
Name : val2, Type : System.Int64
--------------
Max with following parameters
Name : val1, Type : System.UInt64
Name : val2, Type : System.UInt64
--------------
Max with following parameters
Name : val1, Type : System.Single
Name : val2, Type : System.Single
--------------
Max with following parameters
Name : val1, Type : System.Double
Name : val2, Type : System.Double
--------------
Max with following parameters
Name : val1, Type : System.Decimal
Name : val2, Type : System.Decimal
--------------
Min with following parameters
Name : val1, Type : System.SByte
Name : val2, Type : System.SByte
--------------
Min with following parameters
Name : val1, Type : System.Byte
Name : val2, Type : System.Byte
--------------
Min with following parameters
Name : val1, Type : System.Int16
Name : val2, Type : System.Int16
--------------
Min with following parameters
Name : val1, Type : System.UInt16
Name : val2, Type : System.UInt16
--------------
Min with following parameters
Name : val1, Type : System.Int32
Name : val2, Type : System.Int32
--------------
Min with following parameters
Name : val1, Type : System.UInt32
Name : val2, Type : System.UInt32
--------------
Min with following parameters
Name : val1, Type : System.Int64
Name : val2, Type : System.Int64
--------------
Min with following parameters
Name : val1, Type : System.UInt64
Name : val2, Type : System.UInt64
--------------
Min with following parameters
Name : val1, Type : System.Single
Name : val2, Type : System.Single
--------------
Min with following parameters
Name : val1, Type : System.Double
Name : val2, Type : System.Double
--------------
Min with following parameters
Name : val1, Type : System.Decimal
Name : val2, Type : System.Decimal
--------------
Log with following parameters
Name : a, Type : System.Double
Name : newBase, Type : System.Double
--------------
Sign with following parameters
Name : value, Type : System.SByte
--------------
Sign with following parameters
Name : value, Type : System.Int16
--------------
Sign with following parameters
Name : value, Type : System.Int32
--------------
Sign with following parameters
Name : value, Type : System.Int64
--------------
Sign with following parameters
Name : value, Type : System.Single
--------------
Sign with following parameters
Name : value, Type : System.Double
--------------
Sign with following parameters
Name : value, Type : System.Decimal
--------------
BigMul with following parameters
Name : a, Type : System.Int32
Name : b, Type : System.Int32
--------------
DivRem with following parameters
Name : a, Type : System.Int32
Name : b, Type : System.Int32
Name : result, Type : System.Int32&
--------------
DivRem with following parameters
Name : a, Type : System.Int64
Name : b, Type : System.Int64
Name : result, Type : System.Int64&
--------------
试试这个:
MethodInfo[] methodInfos = typeof(Math).GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (MethodInfo methodInfo in methodInfos)
{
Console.WriteLine(methodInfo.Name);
}
另外,如果您还需要知道所有参数,请尝试以下方法
private static void Main()
{
MethodInfo[] methodInfos = typeof(Math).GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (MethodInfo methodInfo in methodInfos)
{
Console.WriteLine(String.Format("{0} with following parameters", methodInfo.Name));
ParameterInfo[] parameters = methodInfo.GetParameters();
foreach (ParameterInfo parameter in parameters)
{
Console.WriteLine("Name : {0}, Type : {1}", parameter.Name, parameter.ParameterType.FullName);
}
Console.WriteLine("--------------");
}
}
输出:
Acos with following parameters
Name : d, Type : System.Double
--------------
Asin with following parameters
Name : d, Type : System.Double
--------------
Atan with following parameters
Name : d, Type : System.Double
--------------
Atan2 with following parameters
Name : y, Type : System.Double
Name : x, Type : System.Double
--------------
Ceiling with following parameters
Name : d, Type : System.Decimal
--------------
Ceiling with following parameters
Name : a, Type : System.Double
--------------
Cos with following parameters
Name : d, Type : System.Double
--------------
Cosh with following parameters
Name : value, Type : System.Double
--------------
Floor with following parameters
Name : d, Type : System.Decimal
--------------
Floor with following parameters
Name : d, Type : System.Double
--------------
Sin with following parameters
Name : a, Type : System.Double
--------------
Tan with following parameters
Name : a, Type : System.Double
--------------
Sinh with following parameters
Name : value, Type : System.Double
--------------
Tanh with following parameters
Name : value, Type : System.Double
--------------
Round with following parameters
Name : a, Type : System.Double
--------------
Round with following parameters
Name : value, Type : System.Double
Name : digits, Type : System.Int32
--------------
Round with following parameters
Name : value, Type : System.Double
Name : mode, Type : System.MidpointRounding
--------------
Round with following parameters
Name : value, Type : System.Double
Name : digits, Type : System.Int32
Name : mode, Type : System.MidpointRounding
--------------
Round with following parameters
Name : d, Type : System.Decimal
--------------
Round with following parameters
Name : d, Type : System.Decimal
Name : decimals, Type : System.Int32
--------------
Round with following parameters
Name : d, Type : System.Decimal
Name : mode, Type : System.MidpointRounding
--------------
Round with following parameters
Name : d, Type : System.Decimal
Name : decimals, Type : System.Int32
Name : mode, Type : System.MidpointRounding
--------------
Truncate with following parameters
Name : d, Type : System.Decimal
--------------
Truncate with following parameters
Name : d, Type : System.Double
--------------
Sqrt with following parameters
Name : d, Type : System.Double
--------------
Log with following parameters
Name : d, Type : System.Double
--------------
Log10 with following parameters
Name : d, Type : System.Double
--------------
Exp with following parameters
Name : d, Type : System.Double
--------------
Pow with following parameters
Name : x, Type : System.Double
Name : y, Type : System.Double
--------------
IEEERemainder with following parameters
Name : x, Type : System.Double
Name : y, Type : System.Double
--------------
Abs with following parameters
Name : value, Type : System.SByte
--------------
Abs with following parameters
Name : value, Type : System.Int16
--------------
Abs with following parameters
Name : value, Type : System.Int32
--------------
Abs with following parameters
Name : value, Type : System.Int64
--------------
Abs with following parameters
Name : value, Type : System.Single
--------------
Abs with following parameters
Name : value, Type : System.Double
--------------
Abs with following parameters
Name : value, Type : System.Decimal
--------------
Max with following parameters
Name : val1, Type : System.SByte
Name : val2, Type : System.SByte
--------------
Max with following parameters
Name : val1, Type : System.Byte
Name : val2, Type : System.Byte
--------------
Max with following parameters
Name : val1, Type : System.Int16
Name : val2, Type : System.Int16
--------------
Max with following parameters
Name : val1, Type : System.UInt16
Name : val2, Type : System.UInt16
--------------
Max with following parameters
Name : val1, Type : System.Int32
Name : val2, Type : System.Int32
--------------
Max with following parameters
Name : val1, Type : System.UInt32
Name : val2, Type : System.UInt32
--------------
Max with following parameters
Name : val1, Type : System.Int64
Name : val2, Type : System.Int64
--------------
Max with following parameters
Name : val1, Type : System.UInt64
Name : val2, Type : System.UInt64
--------------
Max with following parameters
Name : val1, Type : System.Single
Name : val2, Type : System.Single
--------------
Max with following parameters
Name : val1, Type : System.Double
Name : val2, Type : System.Double
--------------
Max with following parameters
Name : val1, Type : System.Decimal
Name : val2, Type : System.Decimal
--------------
Min with following parameters
Name : val1, Type : System.SByte
Name : val2, Type : System.SByte
--------------
Min with following parameters
Name : val1, Type : System.Byte
Name : val2, Type : System.Byte
--------------
Min with following parameters
Name : val1, Type : System.Int16
Name : val2, Type : System.Int16
--------------
Min with following parameters
Name : val1, Type : System.UInt16
Name : val2, Type : System.UInt16
--------------
Min with following parameters
Name : val1, Type : System.Int32
Name : val2, Type : System.Int32
--------------
Min with following parameters
Name : val1, Type : System.UInt32
Name : val2, Type : System.UInt32
--------------
Min with following parameters
Name : val1, Type : System.Int64
Name : val2, Type : System.Int64
--------------
Min with following parameters
Name : val1, Type : System.UInt64
Name : val2, Type : System.UInt64
--------------
Min with following parameters
Name : val1, Type : System.Single
Name : val2, Type : System.Single
--------------
Min with following parameters
Name : val1, Type : System.Double
Name : val2, Type : System.Double
--------------
Min with following parameters
Name : val1, Type : System.Decimal
Name : val2, Type : System.Decimal
--------------
Log with following parameters
Name : a, Type : System.Double
Name : newBase, Type : System.Double
--------------
Sign with following parameters
Name : value, Type : System.SByte
--------------
Sign with following parameters
Name : value, Type : System.Int16
--------------
Sign with following parameters
Name : value, Type : System.Int32
--------------
Sign with following parameters
Name : value, Type : System.Int64
--------------
Sign with following parameters
Name : value, Type : System.Single
--------------
Sign with following parameters
Name : value, Type : System.Double
--------------
Sign with following parameters
Name : value, Type : System.Decimal
--------------
BigMul with following parameters
Name : a, Type : System.Int32
Name : b, Type : System.Int32
--------------
DivRem with following parameters
Name : a, Type : System.Int32
Name : b, Type : System.Int32
Name : result, Type : System.Int32&
--------------
DivRem with following parameters
Name : a, Type : System.Int64
Name : b, Type : System.Int64
Name : result, Type : System.Int64&
--------------
怎么样
怎么样
您是否尝试过此System.Reflection.MethodInfo.GetCurrentMethod()@编码器我不想要当前的方法。我想要类
Math
中存在的所有方法,您想要在运行时还是在设计时使用这些方法?@JohnWillemse runtime您是否尝试过此系统。Reflection.MethodInfo.GetCurrentMethod()@编码器我不想要当前的方法。我想要类Math
中存在的所有方法,你想要在运行时还是设计时使用这些方法?@JohnWillemse RuntimeGood alternative但是这个返回的成员包括(属性、方法、字段、事件等等)。@Mahdi我们可以在一行:)
这个返回的成员包括(属性、方法、字段、事件等)。@Mahdi我们可以在一行代码中完成这项工作。