如何使用Reflection创建通用对象(How do create a Generic Object using Reflection)
首先让我说,我想要做的是获取泛型类中的属性值,该类可以由继承自它的类覆盖。 可以将它在基类中视为默认值,类的继承者可以覆盖它以设置自己的默认值。
我尝试使用System.Reflection.FieldInfo.GetValue直接在类型上使用反射,但这对于具有泛型类型的类不起作用。 所以我认为我需要实例化类才能看到值是什么。
我通过读取bin中的Dll并使用Reflection查找从我的界面继承的类型来检索“类型”。
我使用的是.NET 4.5
这里的文档似乎正好解释了我需要做什么http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx
在这个文档中我唯一的区别是我们如何得到我们的类型,我在bin中查找类型,他们只是调用typeof(),因为类型太复杂了,看起来这可能是一个可能不匹配但我看不到缺少什么(如果有的话)
foreach (var item in types) { var ts = item.GetField("DefaultTimeToExpire"); Type[] typeArguments = item.GetGenericArguments(); if (ts != null) { var t = item.MakeGenericType(typeArguments); var obj = Activator.CreateInstance(t); var timespan = obj.DefaultTimeToExpire; subscriberInfos.Add(new Tuple<string, Type, TimeSpan>(item.Name, item, timespan)); } }
我正在调用GetField来查找具有字段“DefaultTimeToExpire”的项目到目前为止,这部分可以很好地找到我需要的类型。 接下来,我调用GetGenericArguments,它返回Arguments类型的预期数组。 然后我调用MakeGenericType,最后创建实例,给我错误信息
“无法创建BusinessLogic.TestSubscriberXXX`1 [Message]的实例,因为Type.ContainsGenericParameters为true。”
这看起来就像我应该做的那样。 谢谢
First let me say, that what I want to do is get the value of a property in a generic class that may be overriden by class that inherits from it. Think of it in the base class as a default value, that the inheritor of the class can override to set their own Default value.
I have tried to use reflection directly on the type, using the System.Reflection.FieldInfo.GetValue but this does not work for classes with generic types. So I think that I need to instantiate the class to be able to see what the value is.
The "types" I have I retrieved by reading the Dlls in the bin and using Reflection to find the types that inherit from my interface.
I am using .NET 4.5
here is documentation that seems like it explains exactly what I need to do http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx
In this documentation the only difference I can see is how we got our types, I looked in the bin for types and they simply called typeof(), Since types are so complex it seems this may be a likely mis-match but I cannot see what is missing(if anything)
foreach (var item in types) { var ts = item.GetField("DefaultTimeToExpire"); Type[] typeArguments = item.GetGenericArguments(); if (ts != null) { var t = item.MakeGenericType(typeArguments); var obj = Activator.CreateInstance(t); var timespan = obj.DefaultTimeToExpire; subscriberInfos.Add(new Tuple<string, Type, TimeSpan>(item.Name, item, timespan)); } }
I am calling GetField to look for Items that have a field "DefaultTimeToExpire" so far this part works well to find the type I need. Next I call GetGenericArguments which returns an expected array of the type Arguments. then I call MakeGenericType and finally Create instance wich gives me the error message
"Cannot create an instance of BusinessLogic.TestSubscriberXXX`1[Message] because Type.ContainsGenericParameters is true."
This looks like exactly what I am supposed to do. Thanks
原文:https://stackoverflow.com/questions/12168536
最满意答案
通常应该在.yo-rc.json文件中读取应用程序的名称(查看项目的根目录),它是该文件中的“baseName”属性。
例如,如果您的应用程序在第一次运行“yo jhipster”时被命名为“test”,则实体子生成器应该使用该名称并创建“testApp”AngularJS应用程序。
在您的情况下,看起来好像没有在.yo-rc.json文件中读取应用程序名称:您能检查该文件中的内容吗?
Normally the name of your application should be read in your .yo-rc.json file (look at the root of your project), it's the "baseName" attribute in that file.
For example, if your application was named "test" when you first ran "yo jhipster", the entity sub-generator should take that name and create a "testApp" AngularJS application.
In your case, it looks like the application name was not read in the .yo-rc.json file: can you check what's inside that file?
相关问答
更多-
我能想到的最简单的方法是确保你的/ api / players端点返回新创建的实体。 然后,在客户端,只需获取响应,您就可以访问_id属性以供将来使用。 HTH, 麦克风 The easiest way I can think of is to make sure your /api/players endpoint returns the newly created entity. Then, on the client side, simply take the response and you wil ...
-
如果按ID分组,则只能使用以下内容从每个组中选择最新的: public IQueryable
GetCurrentEntities() { return from e in this.Entities group e by e.ID into g select g.OrderByDescending(e => e.CreatedAt).First(); } 您不需要FirstOrDefault()因为除非组中至少有一个Entity ,否则 ... -
我有同样的问题,我的解决方案可能会或可能不适用于您的情况。 就我而言,以前的JHipster版本仅为某些数据库类型生成了hibernate_sequence,不包括用于测试的h2。 检查你的00000000000000_initial_schema.xml liquibase文件,它应该包含创建hibernate_sequence序列的指令,例如:
< ... -
AngularJS和实体框架 - 创建关联而无需复制实体(AngularJS & Entity Framework - Creating association without duplicating entity)[2022-01-28]
我通过确保设置我的实体的状态来修复它。 _uow.UOWContext.Entry(eq).State = System.Data.Entity.EntityState.Modified; 发布的实体的状态是“已分离”,因此我将其设置为已修改,而且工作正常。 I fixed it by making sure to set the state of my entity. _uow.UOWContext.Entry(eq).State = System.Data.Entity.EntityState.M ... -
使用代码首次迁移( 自动或基于代码 )。 如果要将表添加到现有数据库,请先创建初始迁移,然后再添加新实体。 所有链接均适用于EF 4.3,但在EF 5中的用法完全相同。 Use code first migrations (either automatic or code based). If you are adding table to existing database create initial migration first and than add your new entity. All l ...
-
创建一个实体(Creating an entity)[2023-08-16]
通常应该在.yo-rc.json文件中读取应用程序的名称(查看项目的根目录),它是该文件中的“baseName”属性。 例如,如果您的应用程序在第一次运行“yo jhipster”时被命名为“test”,则实体子生成器应该使用该名称并创建“testApp”AngularJS应用程序。 在您的情况下,看起来好像没有在.yo-rc.json文件中读取应用程序名称:您能检查该文件中的内容吗? Normally the name of your application should be read in your ... -
您必须编辑XML并从密钥中删除列。 在
部分(SSDL内容)中找到 标记。 删除 中的任何 ,它实际上不是主键的一部分。 完成此操作后,您可以将设计器中相应标量属性的“实体键”设置为false,EF不会生气。 您也不会被要求在关联中映射此列。 You will have to edit the XML and remove the column from the key. Find the ... -
是的,每个实体都需要id。 它是否映射到视图无关紧要。 也许在您看来,有一些列(复合键)的组合可以用作id? Yes, every entity needs id. Does not matter is it mapped to the view or not. Maybe in your view there is some combination of columns (composite key) that can be used as id?
-
如果不在上下文中,它只是一个简单的类。 但是要小心,假设这个FirstEntity在另一个对象中被引用,如: var FirstEntity = new DBEntity(); var SecondEntity = new DBEntity(); var ThirdEntity = new DBEntity(); DbSet
.Add(SecondEntity); DbSet .Add(ThirdEntity); SecondEntity.Sibling = First ... -
您不能让多个DbSet指向一个DbContext的同一个类。 你的选择是: 创建具有相同属性的新类 遗产 使用不同的DBContext You cannot have multiple DbSet pointing to the same class in one DbContext. Your options are: Creating new class with same properties Inheritance Using different DBContexts