Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Proxy
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv17044/Proxy
Modified Files:
AbstractProxyTypeBuilder.cs
Log Message:
Refactored Stan D. contribution [SPRNET-606]
Index: AbstractProxyTypeBuilder.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Proxy/AbstractProxyTypeBuilder.cs,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** AbstractProxyTypeBuilder.cs 11 Oct 2007 05:57:25 -0000 1.29
--- AbstractProxyTypeBuilder.cs 3 Dec 2007 09:07:18 -0000 1.30
***************
*** 68,72 ****
private IList _typeAttributes = new ArrayList();
private IDictionary _memberAttributes = new Hashtable();
- private readonly IList _typeAttributesToExclude = new ArrayList();
#endregion
--- 68,71 ----
***************
*** 235,242 ****
typeBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
}
else if (attr is Attribute)
{
typeBuilder.SetCustomAttribute(
! ReflectionUtils.CreateCustomAttribute((Attribute) attr));
}
}
--- 234,248 ----
typeBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
}
+ #if NET_2_0
+ else if (attr is CustomAttributeData)
+ {
+ typeBuilder.SetCustomAttribute(
+ ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr));
+ }
+ #endif
else if (attr is Attribute)
{
typeBuilder.SetCustomAttribute(
! ReflectionUtils.CreateCustomAttribute((Attribute)attr));
}
}
***************
*** 258,261 ****
--- 264,274 ----
methodBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
}
+ #if NET_2_0
+ else if (attr is CustomAttributeData)
+ {
+ methodBuilder.SetCustomAttribute(
+ ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr));
+ }
+ #endif
else if (attr is Attribute)
{
***************
*** 264,267 ****
--- 277,281 ----
}
}
+
#if NET_2_0
ApplyMethodReturnTypeAttributes(methodBuilder, targetMethod);
***************
*** 279,298 ****
protected virtual void ApplyMethodReturnTypeAttributes(MethodBuilder methodBuilder, MethodInfo targetMethod)
{
! if (this.ProxyTargetAttributes)
{
! #if !NET_1_0 && !NET_1_1
! // First, try to apply the CustomAttributeData approach
! if (SafeAttributeUtils.ApplyMethodReturnTypeAttributes(methodBuilder, targetMethod))
{
! // if succeeded with CustomAttributeData solution, return.
! return;
}
! #endif
! // If didn't succeed with the CustomAttributeData, apply the old approach
! foreach (Attribute attr in targetMethod.ReturnTypeCustomAttributes.GetCustomAttributes(true))
{
- ParameterBuilder parameterBuilder = methodBuilder.DefineParameter(0, ParameterAttributes.Retval, null);
parameterBuilder.SetCustomAttribute(
! ReflectionUtils.CreateCustomAttribute(attr));
}
}
--- 293,312 ----
protected virtual void ApplyMethodReturnTypeAttributes(MethodBuilder methodBuilder, MethodInfo targetMethod)
{
! foreach (object attr in GetMethodReturnTypeAttributes(targetMethod))
{
! ParameterBuilder parameterBuilder = methodBuilder.DefineParameter(0, ParameterAttributes.Retval, null);
! if (attr is CustomAttributeBuilder)
{
! parameterBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
}
! else if (attr is CustomAttributeData)
{
parameterBuilder.SetCustomAttribute(
! ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr));
! }
! else if (attr is Attribute)
! {
! parameterBuilder.SetCustomAttribute(
! ReflectionUtils.CreateCustomAttribute((Attribute)attr));
}
}
***************
*** 308,331 ****
protected virtual void ApplyMethodParameterAttributes(MethodBuilder methodBuilder, MethodInfo targetMethod)
{
! if (this.ProxyTargetAttributes)
{
! foreach (ParameterInfo paramInfo in targetMethod.GetParameters())
{
! #if !NET_1_0 && !NET_1_1
! // First, try to apply the CustomAttributeData approach
! if (SafeAttributeUtils.ApplyMethodParameterAttributes(methodBuilder, targetMethod,
! paramInfo))
{
! // if succeeded with CustomAttributeData solution, continue with the
! // next parameter.
! continue;
}
#endif
! // If didn't succeed with the CustomAttributeData, apply the old approach
! foreach (Attribute attr in paramInfo.GetCustomAttributes(true))
{
- ParameterBuilder parameterBuilder = methodBuilder.DefineParameter((paramInfo.Position + 1), paramInfo.Attributes, paramInfo.Name);
parameterBuilder.SetCustomAttribute(
! ReflectionUtils.CreateCustomAttribute(attr));
}
}
--- 322,346 ----
protected virtual void ApplyMethodParameterAttributes(MethodBuilder methodBuilder, MethodInfo targetMethod)
{
! foreach (ParameterInfo paramInfo in targetMethod.GetParameters())
{
! foreach (object attr in GetMethodParameterAttributes(targetMethod, paramInfo))
{
! ParameterBuilder parameterBuilder = methodBuilder.DefineParameter(
! (paramInfo.Position + 1), paramInfo.Attributes, paramInfo.Name);
! if (attr is CustomAttributeBuilder)
{
! parameterBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
! }
! #if NET_2_0
! else if (attr is CustomAttributeData)
! {
! parameterBuilder.SetCustomAttribute(
! ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr));
}
#endif
! else if (attr is Attribute)
{
parameterBuilder.SetCustomAttribute(
! ReflectionUtils.CreateCustomAttribute((Attribute)attr));
}
}
***************
*** 345,367 ****
protected virtual IList GetTypeAttributes(Type type)
{
- // TODO : attributes override
-
ArrayList attributes = new ArrayList();
if (this.ProxyTargetAttributes)
{
! #if !NET_1_0 && !NET_1_1
! // Try the CustomAttributeData approach first
! CustomAttributeBuilder[] attrBuilders =
! SafeAttributeUtils.GetMemberAttributes(type, TypeAttributesToExclude);
!
! if (attrBuilders != null)
{
! attributes.AddRange(attrBuilders);
}
! else // If CustomAttributeData approach failed use the old solution
{
!
! // add attributes that apply to the target type
attributes.AddRange(type.GetCustomAttributes(false));
}
--- 360,379 ----
protected virtual IList GetTypeAttributes(Type type)
{
ArrayList attributes = new ArrayList();
if (this.ProxyTargetAttributes)
{
! // add attributes that apply to the target type
! #if NET_2_0
! try
{
! foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(type))
! {
! attributes.Add(cad);
! }
}
! catch(CustomAttributeFormatException)
{
! // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=161522
attributes.AddRange(type.GetCustomAttributes(false));
}
***************
*** 369,376 ****
attributes.AddRange(type.GetCustomAttributes(false));
#endif
-
}
! // add specified attributes
attributes.AddRange(TypeAttributes);
--- 381,387 ----
attributes.AddRange(type.GetCustomAttributes(false));
#endif
}
! // add attributes defined by configuration
attributes.AddRange(TypeAttributes);
***************
*** 390,412 ****
protected virtual IList GetMethodAttributes(MethodInfo method)
{
- // TODO : attributes override
-
ArrayList attributes = new ArrayList();
if (this.ProxyTargetAttributes)
{
! #if !NET_1_0 && !NET_1_1
! // Try the CustomAttributeData approach first
! CustomAttributeBuilder[] attrBuilders =
! SafeAttributeUtils.GetMemberAttributes(method);
!
! if (attrBuilders != null)
! {
! attributes.AddRange(attrBuilders);
! }
! else // If CustomAttributeData approach failed use the old solution
{
! // add attributes that apply to the target method
! attributes.AddRange(method.GetCustomAttributes(false));
}
#else
--- 401,413 ----
protected virtual IList GetMethodAttributes(MethodInfo method)
{
ArrayList attributes = new ArrayList();
if (this.ProxyTargetAttributes)
{
! // add attributes that apply to the target method
! #if NET_2_0
! foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(method))
{
! attributes.Add(cad);
}
#else
***************
*** 434,437 ****
--- 435,546 ----
}
+ #if NET_2_0
+ /// <summary>
+ /// Calculates and returns the list of attributes that apply to the
+ /// specified method's return type.
+ /// </summary>
+ /// <param name="method">The method to find attributes for.</param>
+ /// <returns>
+ /// A list of custom attributes that should be applied to method's return type.
+ /// </returns>
+ /// <see cref="IProxyTypeBuilder.ProxyTargetAttributes"/>
+ protected virtual IList GetMethodReturnTypeAttributes(MethodInfo method)
+ {
+ ArrayList attributes = new ArrayList();
+
+ if (this.ProxyTargetAttributes)
+ {
+ // add attributes that apply to the target method' return type
+ foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(method.ReturnParameter))
+ {
+ attributes.Add(cad);
+ }
+ }
+
+ // TODO: add attributes defined by configuration
+
+ return attributes;
+ }
+ #endif
+
+ /// <summary>
+ /// Calculates and returns the list of attributes that apply to the
+ /// specified method's parameters.
+ /// </summary>
+ /// <param name="method">The method to find attributes for.</param>
+ /// <param name="paramInfo">The method's parameter to find attributes for.</param>
+ /// <returns>
+ /// A list of custom attributes that should be applied to the specified method's parameter.
+ /// </returns>
+ /// <see cref="IProxyTypeBuilder.ProxyTargetAttributes"/>
+ protected virtual IList GetMethodParameterAttributes(MethodInfo method, ParameterInfo paramInfo)
+ {
+ ArrayList attributes = new ArrayList();
+
+ if (this.ProxyTargetAttributes)
+ {
+ // add attributes that apply to the target method's parameter
+ #if NET_2_0
+ foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(paramInfo))
+ {
+ attributes.Add(cad);
+ }
+ #else
+ attributes.AddRange(paramInfo.GetCustomAttributes(false));
+ #endif
+ }
+
+ // TODO: add attributes defined by configuration
+
+ return attributes;
+ }
+
+ /// <summary>
+ /// Check that the specified object is matching the passed attribute type.
+ /// </summary>
+ /// <remarks>
+ /// <p>
+ /// The specified object can be of different type :
+ /// </p>
+ /// <list type="bullet">
+ /// <item>
+ /// <see cref="System.Attribute"/>
+ /// </item>
+ /// <item>
+ /// System.Reflection.CustomAttributeData (Only with .NET 2.0)
+ /// </item>
+ /// <item>
+ /// <see cref="System.Reflection.Emit.CustomAttributeBuilder"/>
+ /// </item>
+ /// </list>
+ /// </remarks>
+ /// <param name="attr">The object instance to check.</param>
+ /// <param name="attrType">The attribute type to test against.</param>
+ /// <returns>
+ /// <see langword="true"/> if the object instance matches the attribute type;
+ /// otherwise <see langword="false"/>.
+ /// </returns>
+ protected virtual bool IsAttributeMatchingType(object attr, Type attrType)
+ {
+ if (attr is Attribute)
+ {
+ return (attrType == attr.GetType());
+ }
+ #if NET_2_0
+ else if (attr is CustomAttributeData)
+ {
+ return (attrType == ((CustomAttributeData)attr).Constructor.DeclaringType);
+ }
+ #endif
+ else if (attr is CustomAttributeBuilder)
+ {
+ return (attrType == ((ConstructorInfo)CustomAttributeConstructorField.GetValue(attr)).DeclaringType);
+ }
+ return false;
+ }
+
+ private static readonly FieldInfo CustomAttributeConstructorField =
+ typeof(CustomAttributeBuilder).GetField("m_con", BindingFlags.Instance | BindingFlags.NonPublic);
+
#endregion
***************
*** 805,825 ****
#endregion
-
- #region Protected Properties
-
- /// <summary>
- /// Gets the type attributes to exclude.
- /// </summary>
- /// <value>The type attributes to exclude.</value>
- protected virtual IList TypeAttributesToExclude
- {
- get
- {
- return _typeAttributesToExclude;
- }
- }
-
- #endregion
-
}
}
\ No newline at end of file
--- 914,917 ----
|