我有一个MyObject带有property(MyProperty)的object ().我想要得到它的类型名称(即String或MyClass等).我用:
PropertyInfo propInfo = typeof(MyObject).GetProperty("MyProperty");
Console.WriteLine(propInfo.PropertyType.Name);
Console.WriteLine(propInfo.PropertyType.FullName);
Run Code Online (Sandbox Code Playgroud)
简单类型没有问题,但是当MyProperty它是泛型类型时,我在获取它的名字方面遇到了问题(例如Collection<String>).它打印:
Collection`1
System.Collections.ObjectModel.Collection`1 [[System.String,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]
那是什么`1?我怎样才能获得" Collection<String>"?
我想使用反射获取属性类型.这是我的代码
var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
model.ModelProperties.Add(
new KeyValuePair<Type, string>
(propertyInfo.PropertyType.Name,
propertyInfo.Name)
);
}
Run Code Online (Sandbox Code Playgroud)
这段代码没问题propertyInfo.PropertyType.Name,但如果我的属性类型是Nullable我得到这个Nullable'1字符串,FullName如果得到这个搅拌System.Nullable1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
我正在将Web API帮助页面与Web API 2(5.0)一起使用-均为最新的Nuget软件包。我希望帮助文档在HttpResponseMessage的正文中显示作为参数或返回的类的属性的注释。
例如,我有一个像这样的控制器方法:
public HttpResponseMessage Post([FromBody] MyClassType1 myClass)
{
// Business logic removed for clarity
return Request.CreateResponse(HttpStatusCode.OK, new MyClassType2());
}
Run Code Online (Sandbox Code Playgroud)
我想要上面发布操作的XML注释,MyClassType1并将MyClassType2其显示在帮助页面上。
我看过的所有地方,到目前为止似乎还不支持。但是,我想知道是否有人能够通过扩展ApiExplorer,添加到XmlDocumentationProvider等使它正常工作?
我知道注释和属性包含在生成的XML文件中,因此我可以尝试手动解析(所有参数和返回类型都在MyAssemblyName.Models名称空间中,因此我想我可以查找具有但是,我知道内置的Web API帮助页面具有某些缓存功能,因此,我宁愿以某种方式将其与现有功能合并(只需对其进行添加)。
通过将Parameters.cshtml模板更新为以下内容,我设法显示了参数的类型(仅向下一层):
@using System.Reflection
@using System.Threading
@using System.Web.Http.Description
@using Regency.API.Services.Areas.HelpPage
@model System.Collections.ObjectModel.Collection<ApiParameterDescription>
<table class="help-page-table">
<thead>
<tr><th>Name</th><th>Properties</th><th>Description</th><th>Additional information</th></tr>
</thead>
<tbody>
@foreach (ApiParameterDescription parameter in Model)
{
string parameterDocumentation = parameter.Documentation ?? "No documentation available.";
Type parameterType = parameter.ParameterDescriptor.ParameterType;
// Don't show CancellationToken because it's a special parameter
if (!typeof (CancellationToken).IsAssignableFrom(parameter.ParameterDescriptor.ParameterType)) …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc asp.net-web-api asp.net-mvc-apiexplorer asp.net-web-api-helppages
GetType().ToString() 返回对象的 FullName。我想要您通常用来实例化该对象的名称,即 int 而不是 Int32。有没有办法做到这一点?