tob*_*ter 11 c# reflection office-interop
我尝试将基于反射的后期绑定库实现到Microsoft Office.Offce COM对象的属性和方法按以下方式调用:
Type type = Type.GetTypeFromProgID("Word.Application");
object comObject = Activator.CreateInstance(type);
type.InvokeMember(<METHOD NAME>, <BINDING FLAGS>, null, comObject, new object[] { <PARAMS>});
Run Code Online (Sandbox Code Playgroud)
InvokeMember是唯一可能的方法,因为Type.GetMethod/GetProperty与COM对象不能正常工作.
可以使用InvokeMember调用方法和属性,但现在我必须解决以下问题:
office-interop包装器中的方法:
Excel.Workbooks wb = excel.Workbooks;
Excel.Workbook firstWb = wb[0];
Run Code Online (Sandbox Code Playgroud)
分别
foreach(Excel.Workbook w in excel.Workbooks)
// doSmth.
Run Code Online (Sandbox Code Playgroud)
如何通过反射调用Excel.Workbooks的this [int index]运算符?
我可能会误解你的问题,但希望这有助于一些人.
当你有一个工作簿时,这将得到第n个工作簿:
typeof(Workbooks).GetMethod("get_Item").Invoke(excel.Workbooks, new object[] { n });
Run Code Online (Sandbox Code Playgroud)
GetMethod
似乎对我来说很费劲,你使用的是什么版本的.NET?
否则这可能会起作用:
typeof(Workbooks).InvokeMember("Item", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, excel.Workbooks, new object[] { n });
Run Code Online (Sandbox Code Playgroud)
这个(Count)也非常有用:
typeof(Workbooks).InvokeMember("Count", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, excel.Workbooks, null).
Run Code Online (Sandbox Code Playgroud)
要获取工作簿,如果type是excel类型:
type.InvokeMember("Workbooks", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, excel.Workbooks, null)
Run Code Online (Sandbox Code Playgroud)