1 // MemberInfoSerializationHolder.cs.cs
5 // Robert Jordan <robertj@gmx.net>
7 // (C) 2003 Patrik Torstensson
10 // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System
.Runtime
.Serialization
;
35 namespace System
.Reflection
38 internal class MemberInfoSerializationHolder
: IObjectReference
, ISerializable
40 const BindingFlags DefaultBinding
= BindingFlags
.Instance
| BindingFlags
.Static
|
41 BindingFlags
.Public
| BindingFlags
.NonPublic
;
43 readonly string _memberName
;
44 readonly string _memberSignature
;
45 readonly MemberTypes _memberType
;
46 readonly Type _reflectedType
;
47 readonly Type
[] _genericArguments
;
49 MemberInfoSerializationHolder(SerializationInfo info
, StreamingContext ctx
)
54 assemblyName
= info
.GetString("AssemblyName");
55 typeName
= info
.GetString("ClassName");
57 _memberName
= info
.GetString("Name");
58 _memberSignature
= info
.GetString("Signature");
59 _memberType
= (MemberTypes
) info
.GetInt32("MemberType");
62 _genericArguments
= null;
64 // FIXME: this doesn't work at present. It seems that
65 // ObjectManager doesn't cope with nested IObjectReferences.
66 // _genericArguments = (Type[]) info.GetValue("GenericArguments", typeof(Type[]));
67 } catch (SerializationException
) {
68 // expected (old NET_1_0 protocol)
72 Assembly asm
= Assembly
.Load(assemblyName
);
74 _reflectedType
= asm
.GetType(typeName
, true, true);
77 public static void Serialize(SerializationInfo info
, String name
, Type klass
, String signature
, MemberTypes type
)
79 Serialize (info
, name
, klass
, signature
, type
, null);
82 public static void Serialize(SerializationInfo info
, String name
, Type klass
, String signature
, MemberTypes type
, Type
[] genericArguments
)
84 info
.SetType( typeof(MemberInfoSerializationHolder
));
86 info
.AddValue("AssemblyName", klass
.Module
.Assembly
.FullName
, typeof(String
));
87 info
.AddValue("ClassName", klass
.FullName
, typeof(String
));
89 info
.AddValue("Name", name
, typeof(String
));
90 info
.AddValue("Signature", signature
, typeof(String
));
91 info
.AddValue("MemberType",(int)type
);
92 info
.AddValue("GenericArguments", genericArguments
, typeof (Type
[]));
95 public void GetObjectData(SerializationInfo info
, StreamingContext context
)
97 throw new NotSupportedException();
100 public object GetRealObject(StreamingContext context
)
104 case MemberTypes
.Constructor
:
105 ConstructorInfo
[] ctors
;
107 ctors
= _reflectedType
.GetConstructors (DefaultBinding
);
108 for (int i
= 0; i
< ctors
.Length
; i
++)
109 if ( ctors
[i
].ToString().Equals(_memberSignature
))
112 throw new SerializationException (String
.Format ("Could not find constructor '{0}' in type '{1}'", _memberSignature
, _reflectedType
));
114 case MemberTypes
.Method
:
115 MethodInfo
[] methods
;
117 methods
= _reflectedType
.GetMethods(DefaultBinding
);
118 for (int i
= 0; i
< methods
.Length
; i
++)
119 if ((methods
[i
]).ToString().Equals(_memberSignature
))
121 else if (_genericArguments
!= null &&
122 methods
[i
].IsGenericMethod
&&
123 methods
[i
].GetGenericArguments().Length
== _genericArguments
.Length
) {
125 MethodInfo mi
= methods
[i
].MakeGenericMethod(_genericArguments
);
127 if (mi
.ToString() == _memberSignature
)
131 throw new SerializationException (String
.Format ("Could not find method '{0}' in type '{1}'", _memberSignature
, _reflectedType
));
133 case MemberTypes
.Field
:
134 FieldInfo fi
= _reflectedType
.GetField (_memberName
, DefaultBinding
);
139 throw new SerializationException (String
.Format ("Could not find field '{0}' in type '{1}'", _memberName
, _reflectedType
));
141 case MemberTypes
.Property
:
142 PropertyInfo pi
= _reflectedType
.GetProperty (_memberName
, DefaultBinding
);
147 throw new SerializationException (String
.Format ("Could not find property '{0}' in type '{1}'", _memberName
, _reflectedType
));
149 case MemberTypes
.Event
:
150 EventInfo ei
= _reflectedType
.GetEvent (_memberName
, DefaultBinding
);
155 throw new SerializationException (String
.Format ("Could not find event '{0}' in type '{1}'", _memberName
, _reflectedType
));
158 throw new SerializationException (String
.Format ("Unhandled MemberType {0}", _memberType
));