Access object properties dynamically

Sometimes there is a situation that you  have some object  and there’s is no possibility to cat it into it’s into original object type ..You can consider one example like I  had faced.. You have to retrieve an excel object from your RTO …..  and now you  want to access the properties and workbook and call all your favourite methods from this object … but . ooopssssss.. you have no reference of Microsoft office assemblies .. so here is a way to access all  you want form  an object …

I have created a sample example to explain these late BindingFlag. You can off course download the sample code from here

So without investing more time here is a quick view of the code attached…



MyObjects.AnyObject obj = MyObjects.AnyObject.GetInstance();

 object resp1 = obj.GetType().InvokeMember(“GetData”, (BindingFlags.InvokeMethod), null, obj, new object[] { });

 object resp2 = resp1.GetType().InvokeMember(“SetData”, (BindingFlags.InvokeMethod), null, obj, new object[] { 20, “Dilip” });

 object resp3 = resp2.GetType().InvokeMember(“GetData”, (BindingFlags.InvokeMethod), null, obj, new object[] { });

fi_ID = resp3.GetType().GetProperty(“ID”, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic);

fi_Name = resp3.GetType().GetProperty(“Name”, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic);

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s