Is it possible to reorganize this into one method

I have a bunch of methods that look like this:

public void SourceInfo_Get() { MethodInfo mi = pFBlock.SourceInfo.GetType().GetMethod("SendGet"); if (mi != null) { ParameterInfo[] piArr = mi.GetParameters(); if (piArr.Length == 0) { mi.Invoke(pFBlock.SourceInfo, new object[0]); } } } public void SourceAvailable_Get() { MethodInfo mi = pFBlock.SourceAvailable.GetType().GetMethod("SendGet"); if (mi != null) { ParameterInfo[] piArr = mi.GetParameters(); if (piArr.Length == 0) { mi.Invoke(pFBlock.SourceAvailable, new object[0]); } } } 

I have one method for each property in my pFBlock object. with such a small change between the methods, I feel that there should be a better way to do this, but I cannot think of anything. I am using VS 2005.

+6
c #
source share
5 answers

What about 3 methods?

 public void SourceInfo_Get() { SendGet(pFBlock.SourceInfo); } public void SourceAvailable_Get() { SendGet(pFBlock.SourceAvailable); } private void SendGet(Object obj) { MethodInfo mi = obj.GetType().GetMethod("SendGet"); if (mi != null) { ParameterInfo[] piArr = mi.GetParameters(); if (piArr.Length == 0) { mi.Invoke(obj, new object[0]); } } } 

The idea here is to add a helper method with which you can pass a parameter. You can then use the helper method in your other methods to drastically reduce code.

+7
source share

You could reorganize the duplicate code to something like this

 private void Source_Get( Object source ) { MethodInfo mi = source.GetType().GetMethod("SendGet"); if (mi != null) { ParameterInfo[] piArr = mi.GetParameters(); if (piArr.Length == 0) { mi.Invoke(source, new object[0]); } } } public void SourceInfo_Get() { Source_Get(pFBlock.SourceInfo); } public void SourceAvailable_Get() { Source_Get(pFBlock.SourceAvailable) } 
0
source share
 public void SourceAvailable_Get() { CallMethod(pFBlock.SourceAvailable); } private void CallMethod(object source, String methodName = "SendGet") { MethodInfo mi = source.GetType().GetMethod(methodName); if (mi != null) { if (mi.GetParameters().Length == 0) { mi.Invoke(source, new object[0]); } } } 

or we use Dynamic ...

 public void CallSendGet(object obj) { obj.AsDynamic().SendGet(); } 
0
source share
  public void Source_Get(string memberName) { object member = pFBlock.GetMember(memberName); MethodInfo mi = member.GetType().GetMethod("SendGet"); if (mi != null) { ParameterInfo[] piArr = mi.GetParameters(); if (piArr.Length == 0) { mi.Invoke(member, new object[0]); } } } 

Sometimes, individual functions make the code more readable. You should not combine your functions as much as possible.

0
source share

Try it. Typed not verified.

 private void MasterGet(PFBlock pFBlock, string propertyName) { Type t = pFBlock.GetType(); // Cycle through the properties. foreach (PropertyInfo p in t.GetProperties()) { if(p.Name == propertyName) { MethodInfo mi = p.GetType().GetMethod("SendGet"); if (mi != null) { ParameterInfo[] piArr = mi.GetParameters(); if (piArr.Length == 0) { mi.Invoke(p, new object[0]); } } } } } 
0
source share

All Articles