10

プログラムを実行していて、リフレクションを実行したいのですが、そのためには Type クラスのオブジェクトが必要ですよね? .GetProperties() メソッドを使用するには...だから私はこれを試しました:

Type typeName = simObjects.getType();

しかし、.GetType() は「System.__COMObject」を返しています。そして、これは役に立ちません。.typeof() でも同じことが起こります。別のコードを検索して見つけました。これは次のコードです。

Type typeName = (Type)Microsoft.VisualBasic.Information.TypeName(simObjects);

しかし、このメソッドは文字列を返します。私は System.Type でそれを必要としています。

4

2 に答える 2

5

思ったようにリフレクションを使用しませんでしたが、かなりうまく機能しています。

foreach(PropertyDescriptor descrip in TypeDescriptor.GetProperties(COMObject))
{
    if(descrip.Name == "Attribute Name")
    {
        foreach(PropertyDescriptor descrip2 in TypeDescriptor.GetProperties(descrip))
        {
           if(descrip2.Name == "sub attribute Name")
           {
           }
        } 
    }
}

このコードは属性の名前を返します。たとえば、COMObject に次の属性があるとします。

int age;
string name;
Son Phill;

そして息子は:

int age;
string name;

最初のループでは、descrip.Name は "age"、"name"、および "Phill" になり、2 番目のループでは (条件が "Son" に対して true を返すと仮定します)、"age" および "name" になります。

于 2014-12-17T12:38:16.893 に答える
1

See this link on how to get the type:

http://support.microsoft.com/kb/320523

See this SO answer regarding COM objects and reflection:

https://stackoverflow.com/a/10617479/4004002

Also, do you know what the properties are ahead of time? If so you may (I've never tried it with a COM object) be able to use Dynamics instead to access the properties.

dynamic d = simObjects;
string myVariable = d.SomeProperty;

EDIT: This link explains using dynamics and COM

http://msdn.microsoft.com/en-us/magazine/ff714583.aspx

In case it disappears:

public static class WordDocument
{
    public const String TemplateName = @"Sample.dotx";
    public const String CurrentDateBookmark = "CurrentDate";
    public const String SignatureBookmark = "Signature";

    public static void Create(string file, DateTime now, String author)
    {
         // Run Word and make it visible for demo purposes
         dynamic wordApp = new Application { Visible = true };

        // Create a new document
        var doc = wordApp.Documents.Add(TemplateName);
        templatedDocument.Activate();

        // Fill the bookmarks in the document
        doc.Bookmarks[CurrentDateBookmark].Range.Select();
        wordApp.Selection.TypeText(current.ToString());
        doc.Bookmarks[SignatureBookmark].Range.Select();
        wordApp.Selection.TypeText(author);
于 2014-09-10T18:24:39.763 に答える