Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following piece of code might be somewhat long but I certainly do think that it does all that you have requested.</p> <p>Let's start with the <em>DynamicInvoke</em> class.</p> <pre><code>public class DynamicInvoke { List&lt;object&gt; Parameters { get; set; } List&lt;string&gt; Paths { get; set; } object Instance { get; set; } Type Type { get; set; } public DynamicInvoke(string path, params object[] parameters) { Parameters = parameters.ToList(); Paths = path.Split('+').ToList(); Type = AppDomain.CurrentDomain .GetAssemblies() .Where(x =&gt; x.GetName().Name == Paths[0]) .First() .GetTypes() .Where(x =&gt; x.FullName == Paths[1]) .First(); Instance = Activator.CreateInstance(Type, Parameters.ToArray()); } public T DynamicPropertyGet&lt;T&gt;() { return (T)Type.GetProperty(Paths[2]).GetValue(Instance, null); } public void DynamicPropertySet(object value) { Type.GetProperty(Paths[2]).SetValue(Instance, value, null); } public T DynamicMethodInvoke&lt;T&gt;(params object[] parameters) { return (T)Type.GetMethods() .Where(x =&gt; x.Name == Paths[2] &amp;&amp; AreAllEqual(x, parameters)) .First() .Invoke(Instance, parameters); } bool AreAllEqual(MethodInfo method, params object[] parameters) { var p1 = method.GetParameters().Select(x =&gt; x.ParameterType); var p2 = parameters.Select(x =&gt; x.GetType()); var except1 = p1.Except(p2).ToList().Count; var except2 = p2.Except(p1).ToList().Count; return (except1 &gt; 0 || except2 &gt; 0) ? false : true; } } </code></pre> <p>As you can see, it contains four properties which will hold all the relevant values we will need to invoke the required properties and methods.</p> <p>In its constructor you simply pass as its first parameter the path to the property or the method you need to invoke. The rest are the required parameters that you might need to instantiate the type on which the property or the method will be invoked. The constructor will then parse the path parameter and provide you with the appropriate instance.</p> <p>You can then either invoke the <em>DynamicPropertyGet()</em> method to get the value from some property, <em>DynamicPropertySet(object)</em> method to set the value of some property, or the <em>DynamicMethodInvoke(params object[])</em> to invoke a method. The generic parameters are used to designate the types of the instances that will be returned.</p> <p>The <em>AreAllEqual(MethodInfo, params object[])</em> method is simply here to decide which overloaded method to invoke, depending on the parameter count and type.</p> <p>The following is our test class. You can notice that it defines both properties and overloaded methods.</p> <pre><code>class People { public string Name { get; set; } public People() { Name = "Billy"; } public People(string name) { Name = name; } public string CallMe() { return Name; } public string CallMe(string value) { return value; } public void NoReturn() { Console.WriteLine("nothing"); } } </code></pre> <p>You can now test this approach with the following lines of code.</p> <pre><code>class Program { static void Main() { var path = "Types+Types.People+Name"; var path2 = "Types+Types.People+CallMe"; var path3 = "Types+Types.People+NoReturn"; var instance1 = new DynamicInvoke(path); var instance2 = new DynamicInvoke(path, "Jill"); var instance3 = new DynamicInvoke(path2); var instance4 = new DynamicInvoke(path2, "Johnny"); var instance5 = new DynamicInvoke(path3); instance1.DynamicPropertySet("Tom"); sc.WriteLine(instance1.DynamicPropertyGet&lt;string&gt;()); sc.WriteLine(instance2.DynamicPropertyGet&lt;string&gt;()); sc.WriteLine(instance3.DynamicMethodInvoke&lt;string&gt;()); sc.WriteLine(instance4.DynamicMethodInvoke&lt;string&gt;("Timmy")); instance5.DynamicMethodInvoke&lt;object&gt;(); Console.Read(); } } </code></pre> <p>The paths to the properties and methods are split in three parts using the "+" sign. The 1st part is the name of the assembly you want to use. the 2nd part is the full name of the type you will instantiate. While the 3rd part is the name of the method or the property you want to invoke.</p> <p>You should also notice that each instance variable holds an instance of your type specified in the path and you can modify its properties multiple times by simply invoking the above mentioned methods.</p> <p><strong>EDIT: Inner property access example.</strong></p> <p>The following is a situation that you seem to be dealing with.</p> <pre><code>class University { public Faculty Faculty { get; set; } public University() { Faculty = new Faculty(); } } class Faculty { public string Name { get; set; } public Faculty() { Name = "MIT"; } } </code></pre> <p>Let's say you have a <em>University</em> class, and you have a <em>Faculty</em> class. You can see that you have a <strong>Faculty</strong> property, of type <em>Faculty</em>, defined in the <em>University</em> class. You can also notice that the <em>Faculty</em> class has a <strong>Name</strong> property. This property represents your "<em>AnalyzeFilesFixe‌​dSampleRate</em>" property of type <em>double</em>.</p> <p>To reach this property, you simply have to execute the following lines of code.</p> <pre><code>var path = "Types+Types.University+Faculty"; var instance = new DynamicInvoke(path); Consolec.WriteLine(instance.DynamicPropertyGet&lt;Faculty&gt;().Name); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload