Skip to content

Instantly share code, notes, and snippets.

@cnsoft
Last active December 28, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnsoft/7557501 to your computer and use it in GitHub Desktop.
Save cnsoft/7557501 to your computer and use it in GitHub Desktop.
Dynamic GetMethod and call it!
//client.Photon.SupportClass.
public static List<MethodInfo> GetMethods (Type type, Type attribute)
{
List<MethodInfo> list = new List<MethodInfo> ();
List<MethodInfo> result;
if (type == null)
{
result = list;
}
else
{
MethodInfo[] methods = type.GetMethods (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
MethodInfo[] array = methods;
for (int i = 0; i < array.Length; i++)
{
MethodInfo methodInfo = array [i];
if (attribute == null || methodInfo.IsDefined (attribute, false))
{
list.Add (methodInfo);
}
}
result = list;
}
return result;
}
//
internal protected static bool GetMethod(MonoBehaviour monob, string methodType, out MethodInfo mi)
{
mi = null;
if (monob == null || string.IsNullOrEmpty(methodType))
{
return false;
}
List<MethodInfo> methods = SupportClass.GetMethods(monob.GetType(), null);
for (int index = 0; index < methods.Count; index++)
{
MethodInfo methodInfo = methods[index];
if (methodInfo.Name.Equals(methodType))
{
mi = methodInfo;
return true;
}
}
return false;
}
//call it
internal protected void ExecuteOnSerialize(PhotonStream pStream, PhotonMessageInfo info)
{
if (failedToFindOnSerialize)
{
return;
}
if (OnSerializeMethodInfo == null)
{
if (!NetworkingPeer.GetMethod(this.observed as MonoBehaviour, PhotonNetworkingMessage.OnPhotonSerializeView.ToString(), out OnSerializeMethodInfo))
{
Debug.LogError("The observed monobehaviour (" + this.observed.name + ") of this PhotonView does not implement OnPhotonSerialize()!");
failedToFindOnSerialize = true;
return;
}
}
OnSerializeMethodInfo.Invoke((object)this.observed, new object[] { pStream, info });
}
@cnsoft
Copy link
Author

cnsoft commented Nov 20, 2013

support ios? GetType().GetMethods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment