简体   繁体   English

只有在我的代码中显式访问属性时,才能通过反射访问任务属性

[英]Accessing Task properties via reflection only works if the properties are explicitly accessed in my code

I'm trying to access a property of a Task in an MS Project via System.Type.GetProperty("Finish") , but it looks like the property (and others like it eg Number1 ) is not accessible via reflection unless my code specifically uses that property. 我试图通过System.Type.GetProperty("Finish")访问MS Project中的Task属性,但看起来像属性(和其他类似的东西,例如Number1 )无法通过反射访问,除非我的代码专门使用该属性。 Is this a compiler setting that I can change? 这是我可以更改的编译器设置吗? I'm writing a synchronization engine so it is impossible to tell ahead of time what field of a Task the client system may request. 我正在编写一个同步引擎,因此无法提前告知客户端系统可能请求的Task的哪个字段。

So with the example below info will be null. 因此,通过下面的示例,info将为null。

System.Type ty = typeof(Task);
System.Reflection.PropertyInfo info = ty.GetProperty("Finish");

but with the code below info is returned as a valid PropertyInfo object. 但是使用下面的代码,info将作为有效的PropertyInfo对象返回。

Task tk = activeProject.ProjectSummaryTask;
object done = tk.Finish;
System.Type ty = typeof(Task);
System.Reflection.PropertyInfo info = ty.GetProperty("Finish");

I basically expected GetProperty() to work for all the properties listed in the API for Task (which are quite a few). 我基本上期望GetProperty()适用于API for Task列出的所有属性(这些属性很多)。

Here is a link to the API for the Finish property: https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.msproject.task.finish?view=office-project-server 以下是Finish属性的API链接: https//docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.msproject.task.finish?view = office-project -server

Use the GetField method of the Task object to return the value for a variable field. 使用Task对象的GetField方法返回变量字段的值。

You'll need to identify the field by the internal field ID--use the intrinsic constant (eg pjTaskFinish) or its numerical value (188743716). 您需要通过内部字段ID识别字段 - 使用内部常量(例如pjTaskFinish)或其数值(188743716)。

Alternatively, you can convert friendly field names to constants using the FieldNameToFieldConstant method of the Application object. 或者,您可以使用Application对象的FieldNameToFieldConstant方法将友好字段名称转换为常量。

ps the VBA-version of the documentation contains more information; ps VBA版本的文档包含更多信息; most, if not all, objects/methods/properties are the same. 大多数(如果不是全部)对象/方法/属性是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM