首页 \ 问答 \ Java获取实际的通用参数(Java get actual generic parameters)

Java获取实际的通用参数(Java get actual generic parameters)

我正在使用java 7.当尝试提取派生类的实际泛型参数时,我使用以下代码:

Class<E> cls = (Class<E>) ((ParameterizedType)   
                  getClass().getGenericSuperclass())
                            .getActualTypeArguments()[0];

它通常运行良好,但如果我有多重继承的情况(C扩展B扩展A)而B也是抽象(尚未声明实际的通用参数)我递归地获取基类之前的类(B)并使用B上的这个方法(而不是getClass())[因为如果我在C上使用它,它的超类将不是参数化类型],实际的类型参数显示了泛型类型(我得到T使用它)。

如果有人遇到这个,我会感谢任何帮助或指导。


I'm using java 7. When trying to extract the actual generic parameters of a derived class I use the following code :

Class<E> cls = (Class<E>) ((ParameterizedType)   
                  getClass().getGenericSuperclass())
                            .getActualTypeArguments()[0];

It usually works well, but if I have the case of a multiple inheritance ( C extends B extends A ) and B is also abstract (not yet declaring the actual generic parameters) I recursively get the class before the base one (B) and use this method on B (instead of getClass()) [since if I had used it on C, its superclass wouldn't be a parameterizedType], and the actual type arguments show me generic types (I get T for using this).

If anyone encountered this I'd appreciate any help or guidelines.


原文:https://stackoverflow.com/questions/28714108
更新时间:2023-08-09 06:08

最满意答案

在Startup.cs文件中的ConfigureServices中查找services.AddMvc()语句,然后添加以下代码:

        services.AddMvc()
            .AddJsonOptions(o =>
            {
                o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                o.SerializerSettings.DateFormatString = "dd/MM/yyy hh:mm:ss";
            }
        );

Look for the services.AddMvc() statement in ConfigureServices in the Startup.cs file, and then add the following code:

        services.AddMvc()
            .AddJsonOptions(o =>
            {
                o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                o.SerializerSettings.DateFormatString = "dd/MM/yyy hh:mm:ss";
            }
        );

相关问答

更多
  • $resource不支持原始响应 由于$resource通常用于连接RESTFUL服务,请在格式良好的对象中发送数据,这就是所有API的工作方式。 从原始类型的API发送数据不鼓励人们使用坏模式。 理想情况下,它应该只返回JSON对象。 [HttpPost] public JsonResult Save(TestModel model) { var newId = _myService.CreateItem(model); return Json(new {Id = newId}); } ...
  • 默认情况下,缓存所有MVC方法。 这可能会在更改/调试周期中导致问题。 对于初学者,您可以使用下面的装饰器关闭开发期间的缓存。 [OutputCache(NoStore = true, Duration = 0)] public ActionResult MyMethod() 您可能希望使用web.config中定义的缓存方案来包装所有方法。 这允许通过在开发期间派上用场的一个配置更改来控制高速缓存。
  • 离线访问是一组不同范围的默认消息,电子邮件和配置文件是其中两个。 没有办法更改消息,无法停止请求配置文件和电子邮件范围。 AccessType(在线,离线)并不是您的想法。 脱机访问将返回刷新令牌,以便您以后可以访问其数据。 在线访问意味着您只有在他们在那里时才会访问他们的数据并且您不需要刷新令牌。 Offline access is the default message for a bunch of the different scopes email and profile are two of th ...
  • 尝试返回一个新的JsonResult : return new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = /* you model goes here */, ContentType = "application/json", ContentEncoding = Encoding.UTF8 }; Try returning a new JsonResult: return ...
  • 所以你有Action方法A,在某些情况下,需要从Action方法B返回结果? 为什么不做这样的事情: public JsonResult ActionMethodA() { if(someCondition) return ActionMethodB(); else return new JsonResult(); } public JsonResult ActionMethodB() { // Something } So you have ...
  • 在Startup.cs文件中的ConfigureServices中查找services.AddMvc()语句,然后添加以下代码: services.AddMvc() .AddJsonOptions(o => { o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; o.SerializerSetting ...
  • 尝试使用Cast方法 (from i in jsonResult.Value.Cast() select i) 编辑:更新的答案 from i in ((IQueryable< MyType >)js.Value) select i Try using Cast method (from i in jsonResult.Value.Cast() select i) Edit: updated answer from i in ((IQueryable< MyType > ...
  • 比如说,例如,您希望使用基于另一个字段的选择的值列表来填充jQuery自动完成,因此您无法确定页面加载时的数据。 我通常会在$.ajax调用中调用一个action方法,然后返回一个项目数组来填充自动完成。 例如,这是我的jQuery,一个用于调用的函数和另一个被调用以使用接收的数据填充自动完成的函数: $(function() { $.ajax({ url: '@Url.Action("GetHomes", "Account")', type: "PO ...
  • 将jQuery ajax dataType指定为'JSON'(您希望从服务器返回的数据类型)。 使用$.each(myData, function(idx, imageBase64) { ... }) 。 Specify your jQuery ajax dataType to 'JSON' (the data type you're expecting back from the server). Loop over myData using $.each(myData, function(idx, im ...
  • 最好只在动作中编写id ,不需要所有属性。 试试这个: [HttpPost] public JsonResult Delete(int contactId) { if (ModelState.IsValid) { _contactUsService.Remove(contactId); } return Json(""); } HTML:
    @Html.ActionLink("Delete", "Delete", "Con ...

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)