System.Runtime.Serialization.SerializationCallbacks.GetMethodsByAttribute C# (CSharp) Method

GetMethodsByAttribute() static private method

static private GetMethodsByAttribute ( Type type, Type attr ) : List
type System.Type
attr System.Type
return List
        static List<MethodInfo> GetMethodsByAttribute(Type type, Type attr)
        {
            List<MethodInfo> list = new List<MethodInfo>();

            Type t = type;
            while (t != typeof(object)) {
                int count = 0;

                foreach (MethodInfo mi in t.GetMethods(DefaultBindingFlags)) {
                    if (mi.IsDefined(attr, false)) {
                        list.Add(mi);
                        count++;
                    }
                }

                // FIXME: MS.NET is checking for this with the verifier at assembly load time.
                if (count > 1)
                    throw new TypeLoadException(
                        String.Format("Type '{0}' has more than one method with the following attribute: '{1}'.", type.AssemblyQualifiedName, attr.FullName));

                t = t.BaseType;
            }

            // optimize memory usage
            return list.Count == 0 ? null : list;
        }

Usage Example

コード例 #1
0
 public SerializationCallbacks(Type type)
 {
     this.onSerializingList   = SerializationCallbacks.GetMethodsByAttribute(type, typeof(OnSerializingAttribute));
     this.onSerializedList    = SerializationCallbacks.GetMethodsByAttribute(type, typeof(OnSerializedAttribute));
     this.onDeserializingList = SerializationCallbacks.GetMethodsByAttribute(type, typeof(OnDeserializingAttribute));
     this.onDeserializedList  = SerializationCallbacks.GetMethodsByAttribute(type, typeof(OnDeserializedAttribute));
 }