我在处理XML文档的类中有一个静态方法(相关)(不那么相关).该方法看起来像这样......
public static bool ProcessFormFile(IFormFile formFile, ModelStateDictionary modelState, string fileExtensionToValidate = "docx")
{
//...some logic & stuff
MemberInfo property = typeof(UploadTemplateViewModel).GetProperty(formFile.Name.Substring(formFile.Name.IndexOf(".") + 1));
//... all the rest
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我正在使用反射来获取一些属性UploadTemplateViewModel.问题是我需要使用另一个类来动态这个类,SomeOtherViewModel并在方法中使用它.
我试过用这样的东西......
public static bool ProcessFormFile(IFormFile formFile, ModelStateDictionary modelState, T entity, string fileExtensionToValidate = "docx") where T : class
Run Code Online (Sandbox Code Playgroud)
......但是我知道了Constraints are not allowed on non-generic declarations.这是我一直想要了解更多信息的topice,但这是我第一次在真实情况下使用它.
我怎样才能实现这一目标?将方法从静态更改为公共或类似的东西?提前致谢.
该方法有效,我可以通过反射得到属性,我只需要使用 typeof(somethingInMethodParameters).GetProperty()
你的方法是static/public/whatever并不重要,你在这里看到的错误信息是因为你错过了泛型类型说明符.例如:
public static bool ProcessFormFile<T>(...) where T : class
// ^^^
// Add this
Run Code Online (Sandbox Code Playgroud)
现在你可以T在反射代码中使用:
MemberInfo property = typeof(T).GetProperty(...)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |