2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / tests / test-102.cs
1 using System;
2 using System.Reflection;
3
4 [assembly: AssemblyTitle ("Foo")]
5 [assembly: AssemblyVersion ("1.0.2")]
6
7 namespace N1 {
8                 
9         [AttributeUsage (AttributeTargets.All)]
10         public class MineAttribute : Attribute {
11
12                 public string name;
13                 
14                 public MineAttribute (string s)
15                 {
16                         name = s;
17                 }
18         }
19
20         [AttributeUsage (AttributeTargets.ReturnValue)]
21         public class ReturnAttribute : Attribute {
22
23                 public string name;
24                 
25                 public ReturnAttribute (string s)
26                 {
27                         name = s;
28                 }
29         }
30
31         public interface TestInterface {
32                 void Hello ([Mine ("param")] int i);
33         }       
34
35         public class Foo {      
36                 
37                 int i;
38                 
39                 [Mine ("Foo")]
40                 [return: Return ("Bar")]        
41                 public static int Main ()
42                 {
43                         Type t = typeof (Foo);
44                         foreach (MemberInfo m in t.GetMembers ()){
45                                 if (m.Name == "Main"){
46                                         MethodInfo mb = (MethodInfo) m;
47
48                                         ICustomAttributeProvider p = mb.ReturnTypeCustomAttributes;
49                                         object [] ret_attrs = p.GetCustomAttributes (false);
50
51                                         if (ret_attrs.Length != 1){
52                                                 Console.WriteLine ("Got more than one return attribute");
53                                                 return 1;
54                                         }
55                                         if (!(ret_attrs [0] is ReturnAttribute)){
56                                                 Console.WriteLine ("Dit not get a MineAttribute");
57                                                 return 2;
58                                         }
59
60                                         ReturnAttribute ma = (ReturnAttribute) ret_attrs [0];
61                                         if (ma.name != "Bar"){
62                                                 Console.WriteLine ("The return attribute is not Bar");
63                                                 return 2;
64                                         }
65                                 }
66                         }
67
68                         Type ifType = typeof (TestInterface);
69                         
70                         MethodInfo method = ifType.GetMethod ("Hello", 
71                                                               BindingFlags.Public | BindingFlags.Instance);
72                         
73                         ParameterInfo[] parameters = method.GetParameters();
74                         ParameterInfo param = parameters [0];
75                         
76                         object[] testAttrs = param.GetCustomAttributes (true);
77                         
78                         if (testAttrs.Length != 1)
79                                 return 1;
80                         
81                         return 0;
82                 }
83         }
84 }