Tag: reflection

在C#中列出word文档的属性

我想要一些帮助来检索word文档的属性。 我已经获得了标题,主题和作者。 但我似乎无法获得“Date Last Saved”属性,我不知道如何获取属性名称列表。 我的代码如下所示: using System; using System.Collections.Generic; using System.Text; using Microsoft.Office.Interop; using System.Reflection; using System.IO; namespace MetaDataSorter { class Program { static void Main(string[] args) { String dirName = @”H:\projekt\test raw files”; String fileNameString = @”H:\projekt\raw files\vgahm\1 NTFS\Raw Files\Microsoft Word Document\1-300\FILE006.DOC”; object fileName = (object)fileNameString; object missing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application wordApp = […]

如何从堆栈框架中获取generics参数的类型?

我们应该通过工厂实例化我们的实体,因为它们在客户端和服务器上的设置不同。 我想确保这种情况,但不能让它工作。 public interface IEntityFactory { TEntity Create() where TEntity : new(); } public abstract class Entity { protected Entity() { VerifyEntityIsCreatedThroughFactory(); } [Conditional(“DEBUG”)] private void VerifyEntityIsCreatedThroughFactory() { foreach (var methodBase in new StackTrace().GetFrames().Select(x => x.GetMethod())) { if (!typeof(IEntityFactory).IsAssignableFrom(methodBase.DeclaringType) || methodBase.Name != “Create”) continue; // The generic type is TEnitiy but I want the provided […]

访问T4模板中的自定义属性(VS2012 MVC4)

我正在尝试访问我的自定义属性,如我正在编写的T4控制器模板中附加到我的模型属性的SortableAttribute 。 我已经将List.tt中的类块,程序集和导入复制为样板文件。 首先,我尝试按如下方式转换属性: 然而,这并没有产生积极的结果,因为我的项目命名空间对T4来说是未知的。 为了解决这个问题,我添加了项目的dll并导入了所需的命名空间。 它起初似乎很成功(例如,命名空间导入没有错误),但它仍然没有找到我的属性。 如果我用MyProject.Filters.SortableAttribute替换SortableAttribute ,则错误消息是在MyProject.Filters找不到SortableAttribute 。 为了解决这个问题,我改变了我的代码如下: 我以为我已经中了大奖,但我很快就意识到这个property.GetCustomAttributes(true)返回所有属性但不是我的…… 示例模型: public class MyModel { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] [Display(Name = “Full Name”)] [Sortable] public int Name { get; set; } } SortableAttribute实现: using System; namespace MyProject.Filters { [AttributeUsage(AttributeTargets.Property)] public class SortableAttribute : Attribute { public SortableAttribute(string […]

插件架构中的reflection与属性

我正在开发一个从子目录启动时加载插件的应用程序,目前我通过使用reflection来迭代每个程序集的类型并查找实现IPluginModule接口的公共类。 由于Reflection涉及性能损失,并且我希望在一段时间后会有几个插件,我想知道在程序集级别定义应用的自定义属性是否有用,可以在迭代类型之前进行检查(可能是程序集中的十几种类型,包括IPluginModule的1个实现者。 该属性(如果存在)可以提供返回所需类型或实例的方法,然后迭代类型将只是一个回退机制。 在配置文件中存储类型信息不是一种选择。 这会改善性能,还是与实际从存储加载组件的时间相比无关紧要? 此外,这种用法是否适用于属性?

如何以编程方式创建Func 委托

我有一个小的dependency injection框架,我试图让它动态地解析Lazy实例。 我的想法是做那样的事情: DIContainer.Register(); var lazyCommand = DIContainer.Resolve<Lazy>(); 前几天我读到Autofac能够做到这一点。 我试图设置该Lazy实例的构造函数。 在下一个测试代码中,抛出exception,因为所需的类型构造函数需要一个Func ,但我传递的是一个Func : static readonly Type _lazyType = typeof(Lazy); static Object ResolveTest(Type type) { if (type.IsGenericType && type.GetGenericTypeDefinition() == _lazyType) { var arg = type.GetGenericArguments()[0]; return Activator.CreateInstance(_lazyType.MakeGenericType(arg), new Func(() => ResolveType(arg))); } else return ResolveType(type); } 我不知道如何创建适合Lazy构造函数参数的委托。 任何的想法? 干杯。

尝试获取与Control事件关联的委托时出现问题

我试图将控件序列化为XML,我想序列化他们的事件处理程序。 我正在使用这段代码: Control childCtrl = …. if (childCtrl.GetType() == typeof(Button)) { EventInfo baseEventInfo = childCtrl.GetType().GetEvent(“Click”); EventHandlerList events = typeof(Control).GetProperty(“Events”, BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic).GetValue(childCtrl, null) as EventHandlerList; object eventField = typeof(Control).GetField(“Event” + baseEventInfo.Name, BindingFlags.NonPublic | BindingFlags.Static).GetValue(childCtrl); Delegate aDel = events[eventField]; xmlSerialisedForm.WriteElementString(“Click”, aDel.Method.ToString()); } if (childCtrl.GetType() == typeof(CheckBox)) { EventInfo baseEventInfo = childCtrl.GetType().GetEvent(“CheckedChanged”); EventHandlerList […]

将接口的ProperyInfo与类的PropertyInfo匹配

我使用类似于以下的方法来获取与Type的属性相关的一些预先计算的元数据。 MyData GetProperty(Expression<Func> member) { // Get the property referenced in the lambda expression MemberExpression expression = member.Body as MemberExpression; PropertyInfo property = expression.Member as PropertyInfo; // get the properties in the type T PropertyInfo[] candidates = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); // Find the match foreach (PropertyInfo candidate in candidates) if (candidate == property) return GetMetaData(candidate); […]

使用entity framework4.0中的上下文确定实体的主键

我有几个从我的Entity Framework数据模型生成的实体。 在.edmx文件中,我可以看到标记为主键的属性。 我的POCO /自定义上下文是从这个和T4模板生成的。 我正在寻找一种方法来使用Entity Framework 4.0的reflection找出我的实体的主键。 是否有一个属性我需要设置才能在生成POCO时设置它? 在我的上下文中是否有属性我可以用来深入查找此信息?

如何检查我是否可以使用给定类型的SUM值

在我们的应用程序中,我们有许多AggregatorTypes(从SQL Server复制的想法),即MinAggregator MaxAggregator,CountAggregator和SumAggregator。 现在我应该检查Aggregator是否支持提供的值的类型。 对于伯爵来说,这并不重要。 对于Max和Min,我使用检查IComparable。 但是什么用于SUM? 我尝试过Converter但它也支持字符串。 为什么没有ISummable 🙂 IsNumeric就像检查一样对我来说也不是解决方案。 例如TimeSpan也可以求和。

为什么reflection搜索突然找不到任何东西?

根据这个问题和答案,我有以下代码如何获取ASP.NET C#中请求的文件的MIME类型? 它工作得非常好: static MimeMappingWrapper() { // dirty trick – Assembly.LoadWIthPartialName has been deprecated //Assembly ass = Assembly.LoadWithPartialName(“System.Web”); Assembly ass = Assembly.GetAssembly(typeof (HttpApplication)); Type mimeMappingType = ass.GetType(“System.Web.MimeMapping”); GetMimeMappingMethod = mimeMappingType.GetMethod(“GetMimeMapping”, BindingFlags.Static | BindingFlags.NonPublic); } 现在突然mimeMappingType.GetMethod(“GetMimeMapping”, BindingFlags.Static | BindingFlags.NonPublic)返回null 。 可能是什么原因? 在应用程序中没有更改任何特殊内容,即使它是,它如何影响包装类的这个构造函数?