2016-05-29 5 views
0

反射クラスPropertyCondition(in assembly UIAutomationClient.dll)を使用してオブジェクトを作成したいとします。希望クラスのコンストラクタを得るために、私は次のコードを使用し静的フィールドに値を代入してオブジェクトを動的に作成する方法は?

var assembly = AppDomain.CurrentDomain.GetAssemblies().First(x => x.FullName.Contains("AutomationClient")); 

var propertyConditionType = assembly.DefinedTypes.First(x => x.Name == "PropertyCondition"); 
var automationElementType = assembly.DefinedTypes.First(x => x.Name == "AutomationElement"); 

var automationIdPropertyType = automationElementType.GetField("AutomationIdProperty").FieldType; 
var constructor = propertyConditionType.GetConstructor(new Type[] { automationIdPropertyType, typeof(object) }); 

をしかし、どのように私は、コンストラクタAutomationElement.AutomationIdPropertyに渡すのですか?

ありがとうございます。

答えて

0

あなたが欠けているステップは、コンストラクタに渡す必要がある値を得るために、単純なFieldInfo.GetValueコールです。私はあなたのコードを再構築しました:

var automationIdPropertyField = automationElementType.GetField("AutomationIdProperty"); 

var automationIdPropertyType = automationIdPropertyField.FieldType; 
var automationIdPropertyValue = automationIdPropertyField.GetValue(null); 

var constructor = propertyConditionType.GetConstructor(new[] { automationIdPropertyType, typeof(object) }); 

var obj = constructor.Invoke(new[] {automationIdPropertyValue, ...}); 
+0

ありがとうございました。 –

0

私はそれがautomationId is a string propertyであると信じています。そして、何が必要なのstringsを受け入れるconstructorを見つけることである:

ConstructorInfo ctor = propertyConditionType.GetConstructor(new[] { typeof(string) }); 
object instance = ctor.Invoke(new object[] { automationId }); 

それとも、Activator.CreateInstance Method使用することができます。

のCreateInstance(AppDomainの、文字列、文字列、論理値、のBindingFlags、バインダー、[]、のCultureInfoオブジェクトを、

指定されたリモートドメイン で指定された名前の型のインスタンスを作成します。名前付きアセンブリと最も一致するコンストラクタ を使用します。指定されたパラメータ。

+0

私はデコンパイラでアセンブリを開いて、私は文字列を取るコンストラクタがないと思う。 – thehennyy

+0

いいえ、PropertyConditionクラスには次のコンストラクタがあります。 public PropertyCondition(AutomationPropertyプロパティ、オブジェクト値)。 public PropertyCondition(AutomationPropertyプロパティ、オブジェクト値、PropertyConditionFlagsフラグ)。 –

+0

そのように、 'id'の代わりに' Automation Element'全体のパラメータとして渡すべきでしょうか? https://msdn.microsoft.com/ru-ru/library/ms752331(v=vs.110).aspx –

関連する問題