Tag: system.reflection

获取WPF项目的项目名称

我正在编写我的第一个WPF应用程序,我正在尝试获取项目的名称,以便我可以输出它。 但是,使用 Assembly.GetEntryAssembly().GetName() 要么 Assembly.GetExecutingAssembly().GetName() 获取名称和版本号(即DataPusher,版本= 2.0.466.16967)。 有没有办法只获得程序集名称? 谢谢。

从Attribute转到CustomAttributeData或向后

题。 有没有办法根据我的自定义属性的给定实例获取CustomAttributeData的实例,比如, MyAttribute ? 或相反亦然? 我为什么需要这个? MyAttribute的实例包含我感兴趣的属性,而CustomAttributeData的实例包含我感兴趣的实际构造函数参数。所以现在我实现了双重工作: 首先 ,通过调用获取MyAttribute的实例 Attribute.GetCustomAttribute(property, typeof(MyAttribute)) as MyAttribute , 第二 ,通过调用获取CustomAttributeData的实例 CustomAttributeData.GetCustomAttributes(property) 并走过这个系列。 PS我已经看过这个问题 ,但没有在那里找到理想的解决方案。

exception信息:System.Reflection.TargetInvocationException

我正在研究WPF应用程序,我只在运行时在单个设备上收到此错误。 Exception Info: System.Reflection.TargetInvocationException 我的问题: 任何可能导致此错误的线索? 知道如何在发布模式下调试应用程序吗? Exception Info: System.Reflection.TargetInvocationException Stack: at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[]) at System.Delegate.DynamicInvokeImpl(System.Object[]) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at […]

如何使用动态生成的对象作为CodeEffects生成器的数据源

我们正在使用这个组件www.codeeffects.com,它允许我们根据对象属性创建业务规则。 视图的html是这样的: @{ ViewBag.Title = “Post Example”; Html.CodeEffects().Styles() .SetTheme(ThemeType.Gray) .Render(); } @using (Html.BeginForm(“Evaluate”, “Post”, FormMethod.Post)) { Post Example Info: @ViewBag.Message @{ Html.CodeEffects().RuleEditor() .Id(“ruleEditor”) .SaveAction(“Save”, “Post”) .DeleteAction(“Delete”, “Post”) .LoadAction(“Load”, “Post”) .Mode(RuleType.Execution) .ContextMenuRules(ViewBag.ContextMenuRules) .ToolBarRules(ViewBag.ToolBarRules) .Rule(ViewBag.Rule) .Render(); } Rule Test Form @{ Html.RenderPartial(“_PatientForm”); } } @{ Html.CodeEffects().Scripts().Render(); } 控制器中的索引操作如下: [HttpGet] public ActionResult Index() { ViewBag.Rule = RuleModel.Create(typeof(Patient)); return […]

从Generic Class获取ICollection类型的属性的列表

我有一个包含一些ICollection类型属性的对象 所以基本上这个类看起来像这样: Class Employee { public ICollection Addresses {get;set;} public ICollection Performances {get; set;} } 问题是通过使用reflection获取Generic类内部的ICollection类型的属性名称。 我的通用类是 Class CRUD { public object Get() { var properties = typeof(TEntity).GetProperties().Where(m=m.GetType() == typeof(ICollection ) … } 但它没有用。 我怎样才能在这里获得房产?

typeof(DateTime?)。Name == Nullable`1

在.Net typeof(DateTime?).Name使用Reflection typeof(DateTime?).Name返回“Nullable`1”。 有没有办法将实际类型作为字符串返回。 (在本例中为“DateTime”或“System.DateTime”) 我明白DateTime? 是Nullable 。 除此之外,我只是在寻找可空类型的类型。

如何使用Reflection将构造函数作为MethodInfo获取

构造函数如下所示: public NameAndValue(string name, string value) 我需要使用Reflection将其作为MethodInfo。 它尝试了以下,但它没有找到构造函数( GetMethod返回null )。 MethodInfo constructor = typeof(NameAndValue).GetMethod(“.ctor”, new[] { typeof(string), typeof(string) }); 我究竟做错了什么?

通过reflection获取对象

我正在寻找c#这样的机制: Car car1; Car car2; Car car = (Car)SomeMechanism.Get(“car1”); car1和car2是领域 所以我想用reflection得到一些对象,而不是键入:/我怎么能在c#中做到?

使用EF Core在通用存储库中使用Reflection包含所有导航属性

我正在为EF Core项目创建一个通用存储库,以避免为所有模型编写CRUD。 我遇到的主要障碍是没有加载导航属性,因为Core还不支持延迟加载,而generics类显然无法为类特定属性定义.Include语句。 我正在尝试为我的Get方法执行类似的操作以动态包含所有属性: public virtual T Get(Guid itemId, bool eager = false) { IQueryable querySet = _context.Set(); if (eager) { foreach (PropertyInfo p in typeof(T).GetProperties()) { querySet = querySet.Include(p.Name); } } return querySet.SingleOrDefault(i => i.EntityId == itemId); } 但是,当包含不是导航属性的属性时,它会引发错误。 我发现这个答案与EF 5相同,但它涉及EF核心中不存在的方法: EF5如何获取域对象的导航属性列表 是否有可能在EF Core中完成同样的事情?

CIL“错误”条款与C#中的“catch”条款有何不同?

根据CLI标准 (Partition IIA,第19章)和System.Reflection.ExceptionHandlingClauseOptions枚举的MSDN参考页面,有四种不同的exception处理程序块: catch子句: “捕获指定类型的所有对象。” filter子句: “仅在filter成功时输入处理程序”。 最后条款: “处理所有exception和正常退出。” fault子句: “处理所有exception,但不能正常退出。” 鉴于这些简短的解释(引自CLI标准,顺便说一句),这些应该映射到C#,如下所示: catch – catch (FooException) { … } filter – 在C#中不可用(但在VB.NET中为Catch FooException When booleanExpression ) 终于 – finally { … } 错误 – catch { … } 实验: 一个简单的实验表明,这种映射不是.NET的C#编译器真正做的事情: // using System.Linq; // using System.Reflection; static bool IsCatchWithoutTypeSpecificationEmittedAsFaultClause() { try { return MethodBase .GetCurrentMethod() […]