2 // Base class for CodeGenerator.GenerateCodeFromType unit tests.
5 // Gert Driesen (drieseng@users.sourceforge.net)
12 using System.CodeDom.Compiler;
13 using System.Reflection;
14 using System.Security;
15 using System.Security.Principal;
17 using NUnit.Framework;
19 namespace MonoTests.System.CodeDom.Compiler
21 public abstract class CodeGeneratorFromTypeTestBase : CodeGeneratorTestBase
23 protected abstract CodeTypeDeclaration TypeDeclaration
29 public abstract void DefaultTypeTest ();
32 public abstract void NullTypeTest ();
35 public abstract void SimpleTypeTest ();
38 public abstract void DerivedTypeTest ();
41 public abstract void AttributesAndTypeTest ();
44 public abstract void EventMembersTypeTest1 ();
47 public abstract void EventMembersTypeTest2 ();
50 public abstract void EventImplementationTypes ();
53 public abstract void EventPrivateImplementationType ();
56 public abstract void EventImplementationTypeOrder ();
59 public abstract void FieldMembersAttributesTest ();
62 public abstract void FieldMembersTypeTest ();
65 public abstract void FieldNewSlotTest ();
68 public abstract void PropertyMembersTypeTest1 ();
71 public abstract void PropertyMembersTypeTest2 ();
74 public abstract void PropertyMembersTypeGetOnly ();
77 public abstract void PropertyMembersTypeSetOnly ();
80 public abstract void PropertyMembersTypeGetSet ();
83 public abstract void PropertyMembersTypeFamilyOrAssembly ();
86 public abstract void PropertyMembersTypeAssembly ();
89 public abstract void PropertyParametersTest ();
92 public abstract void PropertyIndexerTest1 ();
95 public abstract void PropertyIndexerTest2 ();
98 public abstract void PropertyIndexerGetOnly ();
101 public abstract void PropertyIndexerSetOnly ();
104 public abstract void PropertyImplementationTypes ();
107 public abstract void PropertyOverloadsTest1 ();
110 public abstract void PropertyOverloadsTest2 ();
113 public abstract void PropertyOverloadsTest3 ();
116 public abstract void PropertyPrivateImplementationType ();
119 public abstract void PropertyImplementationTypeOrder ();
122 public abstract void PropertyNewSlotTest ();
125 public abstract void MethodMembersTypeTest1 ();
128 public abstract void MethodMembersTypeTest2 ();
131 public abstract void MethodMembersTypeTest3 ();
134 public abstract void MethodImplementationTypes ();
137 public abstract void MethodOverloadsTest1 ();
140 public abstract void MethodOverloadsTest2 ();
143 public abstract void MethodOverloadsTest3 ();
146 public abstract void MethodPrivateImplementationType ();
149 public abstract void MethodImplementationTypeOrder ();
152 public abstract void MethodParamArrayAttribute ();
155 public abstract void MethodReturnTypeAttributes ();
158 public abstract void MethodNewSlotTest ();
161 public abstract void ConstructorAttributesTest ();
164 public abstract void ConstructorParametersTest ();
167 public abstract void ConstructorParameterAttributesTest ();
170 public abstract void BaseConstructorSingleArg ();
173 public abstract void BaseConstructorMultipleArgs ();
176 public abstract void ChainedConstructorSingleArg ();
179 public abstract void ChainedConstructorMultipleArgs ();
182 public abstract void TypeConstructorTest ();
185 public abstract void EntryPointMethodTest ();
189 public abstract void PartialTypeTest ();
192 protected string GenerateDefaultType (CodeGeneratorOptions options)
194 return GenerateCodeFromType (TypeDeclaration, options);
197 protected string GenerateNullType (CodeGeneratorOptions options)
199 return GenerateCodeFromType (null, options);
202 protected string GenerateSimpleType (CodeGeneratorOptions options)
204 TypeDeclaration.Name = "Test1";
205 return GenerateCodeFromType (TypeDeclaration, options);
208 protected string GenerateDerivedType (CodeGeneratorOptions options)
210 TypeDeclaration.Name = "Test1";
211 TypeDeclaration.TypeAttributes |= TypeAttributes.NestedFamily |
212 TypeAttributes.Abstract;
213 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (int)));
214 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (IIdentity)));
215 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (string)));
216 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (IPermission)));
217 return GenerateCodeFromType (TypeDeclaration, options);
220 protected string GenerateAttributesAndType (CodeGeneratorOptions options)
222 TypeDeclaration.Name = "Test1";
224 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
226 TypeDeclaration.CustomAttributes.Add (attrDec);
228 attrDec = new CodeAttributeDeclaration ();
230 TypeDeclaration.CustomAttributes.Add (attrDec);
232 return GenerateCodeFromType (TypeDeclaration, options);
235 protected string GenerateEventMembersType1 (CodeGeneratorOptions options)
237 TypeDeclaration.Name = "Test1";
239 CodeMemberEvent evt = new CodeMemberEvent ();
241 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
243 evt.CustomAttributes.Add (attrDec);
245 attrDec = new CodeAttributeDeclaration ();
247 evt.CustomAttributes.Add (attrDec);
249 TypeDeclaration.Members.Add (evt);
251 return GenerateCodeFromType (TypeDeclaration, options);
254 protected string GenerateEventMembersType2 (CodeGeneratorOptions options)
256 TypeDeclaration.Name = "Test1";
258 CodeMemberEvent evt = new CodeMemberEvent ();
260 evt.Attributes = MemberAttributes.Public | MemberAttributes.Override
261 | MemberAttributes.Static | MemberAttributes.Abstract |
262 MemberAttributes.New;
263 evt.Type = new CodeTypeReference (typeof (int));
264 TypeDeclaration.Members.Add (evt);
266 return GenerateCodeFromType (TypeDeclaration, options);
269 protected string GenerateEventImplementationTypes (CodeGeneratorOptions options)
271 TypeDeclaration.Name = "Test1";
273 CodeMemberEvent evt = new CodeMemberEvent ();
275 evt.Attributes = MemberAttributes.FamilyAndAssembly;
276 evt.Type = new CodeTypeReference (typeof (int));
277 evt.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
278 evt.ImplementationTypes.Add (new CodeTypeReference ("IWhatever"));
279 TypeDeclaration.Members.Add (evt);
281 return GenerateCodeFromType (TypeDeclaration, options);
284 protected string GenerateEventPrivateImplementationType (CodeGeneratorOptions options)
286 TypeDeclaration.Name = "Test1";
288 CodeMemberEvent evt = new CodeMemberEvent ();
290 evt.Attributes = MemberAttributes.Family | MemberAttributes.Overloaded;
291 evt.Type = new CodeTypeReference (typeof (int));
292 evt.PrivateImplementationType = new CodeTypeReference (typeof (int));
293 TypeDeclaration.Members.Add (evt);
295 return GenerateCodeFromType (TypeDeclaration, options);
298 protected string GenerateEventImplementationTypeOrder (CodeGeneratorOptions options)
300 TypeDeclaration.Name = "Test1";
302 CodeMemberEvent evt = new CodeMemberEvent ();
304 evt.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
305 evt.Type = new CodeTypeReference (typeof (int));
306 evt.PrivateImplementationType = new CodeTypeReference (typeof (int));
307 evt.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
308 TypeDeclaration.Members.Add (evt);
310 return GenerateCodeFromType (TypeDeclaration, options);
313 protected string GenerateFieldMembersAttributes (CodeGeneratorOptions options)
315 TypeDeclaration.Name = "Test1";
317 CodeMemberField fld = new CodeMemberField ();
319 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
321 fld.CustomAttributes.Add (attrDec);
323 attrDec = new CodeAttributeDeclaration ();
325 fld.CustomAttributes.Add (attrDec);
327 TypeDeclaration.Members.Add (fld);
329 return GenerateCodeFromType (TypeDeclaration, options);
332 protected string GenerateFieldMembersType (MemberAttributes memberAttributes, CodeGeneratorOptions options)
334 TypeDeclaration.Name = "Test1";
336 CodeMemberField fld = new CodeMemberField ();
338 fld.Attributes = memberAttributes;
339 fld.Type = new CodeTypeReference (typeof (int));
340 fld.InitExpression = new CodePrimitiveExpression (2);
341 TypeDeclaration.Members.Add (fld);
343 return GenerateCodeFromType (TypeDeclaration, options);
346 protected string GeneratePropertyMembersAttributes (CodeGeneratorOptions options)
348 TypeDeclaration.Name = "Test1";
350 CodeMemberProperty property = new CodeMemberProperty ();
351 TypeDeclaration.Members.Add (property);
353 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
355 property.CustomAttributes.Add (attrDec);
357 attrDec = new CodeAttributeDeclaration ();
359 property.CustomAttributes.Add (attrDec);
361 return GenerateCodeFromType (TypeDeclaration, options);
364 protected string GenerateAbstractProperty (CodeGeneratorOptions options)
366 TypeDeclaration.Name = "Test1";
367 TypeDeclaration.TypeAttributes = TypeAttributes.Abstract | TypeAttributes.Public;
369 CodeMemberProperty property = new CodeMemberProperty ();
370 property.Name = "Name";
371 property.Attributes = MemberAttributes.Public | MemberAttributes.Abstract;
372 property.HasGet = true;
373 property.HasSet = true;
374 property.Type = new CodeTypeReference (typeof (string));
376 TypeDeclaration.Members.Add (property);
378 return GenerateCodeFromType (TypeDeclaration, options);
381 protected string GenerateStaticProperty (CodeGeneratorOptions options)
383 TypeDeclaration.Name = "Test1";
384 TypeDeclaration.TypeAttributes = TypeAttributes.Public;
386 CodeMemberProperty property = new CodeMemberProperty ();
387 property.Name = "Name";
388 property.Attributes = MemberAttributes.Public | MemberAttributes.Static;
389 property.HasSet = true;
390 property.Type = new CodeTypeReference (typeof (string));
392 TypeDeclaration.Members.Add (property);
394 return GenerateCodeFromType (TypeDeclaration, options);
397 protected string GeneratePropertyMembersType (MemberAttributes memberAttributes, bool hasGet, bool hasSet, CodeGeneratorOptions options)
399 TypeDeclaration.Name = "Test1";
401 CodeMemberProperty property = new CodeMemberProperty ();
402 property.Name = "Name";
403 property.Attributes = memberAttributes;
404 property.HasGet = hasGet;
405 property.HasSet = hasSet;
406 property.Type = new CodeTypeReference (typeof (int));
407 TypeDeclaration.Members.Add (property);
409 return GenerateCodeFromType (TypeDeclaration, options);
412 protected string GeneratePropertyParameters (CodeGeneratorOptions options)
414 TypeDeclaration.Name = "Test1";
416 CodeMemberProperty property = new CodeMemberProperty ();
417 property.Name = "Name";
418 property.Attributes = MemberAttributes.Public;
419 property.Type = new CodeTypeReference (typeof (int));
421 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
422 typeof (object), "value1");
423 property.Parameters.Add (param);
425 param = new CodeParameterDeclarationExpression (
426 typeof (int), "value2");
427 param.Direction = FieldDirection.Ref;
428 property.Parameters.Add (param);
430 TypeDeclaration.Members.Add (property);
432 return GenerateCodeFromType (TypeDeclaration, options);
435 protected string GeneratePropertyIndexer (MemberAttributes memberAttributes, bool hasGet, bool hasSet, bool addParameters, CodeGeneratorOptions options)
437 TypeDeclaration.Name = "Test1";
439 CodeMemberProperty property = new CodeMemberProperty ();
440 // ensure case-insensitive comparison is done on name of property
441 property.Name = "iTem";
442 property.Attributes = memberAttributes;
443 property.HasGet = hasGet;
444 property.HasSet = hasSet;
445 property.Type = new CodeTypeReference (typeof (int));
446 TypeDeclaration.Members.Add (property);
449 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
450 typeof (object), "value1");
451 property.Parameters.Add (param);
453 param = new CodeParameterDeclarationExpression (
454 typeof (int), "value2");
455 param.Direction = FieldDirection.Ref;
456 property.Parameters.Add (param);
459 return GenerateCodeFromType (TypeDeclaration, options);
462 protected string GeneratePropertyImplementationTypes (CodeGeneratorOptions options)
464 TypeDeclaration.Name = "Test1";
466 CodeMemberProperty property = new CodeMemberProperty ();
467 property.Name = "Name";
468 property.Attributes = MemberAttributes.Public;
469 property.Type = new CodeTypeReference (typeof (int));
470 property.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
471 property.ImplementationTypes.Add (new CodeTypeReference ("IWhatever"));
472 TypeDeclaration.Members.Add (property);
474 return GenerateCodeFromType (TypeDeclaration, options);
477 protected string GeneratePropertyOverloads1 (CodeGeneratorOptions options)
479 TypeDeclaration.Name = "Test1";
481 CodeMemberProperty property = new CodeMemberProperty ();
482 property.Name = "Name";
483 property.Attributes = MemberAttributes.Family | MemberAttributes.Overloaded;
484 property.Type = new CodeTypeReference (typeof (int));
485 TypeDeclaration.Members.Add (property);
487 return GenerateCodeFromType (TypeDeclaration, options);
490 protected string GeneratePropertyOverloads2 (CodeGeneratorOptions options)
492 TypeDeclaration.Name = "Test1";
494 CodeMemberProperty property = new CodeMemberProperty ();
495 property.Name = "Name";
496 property.Attributes = MemberAttributes.Public;
497 property.Type = new CodeTypeReference (typeof (int));
498 TypeDeclaration.Members.Add (property);
500 property = new CodeMemberProperty ();
501 property.Name = "Name";
502 property.Attributes = MemberAttributes.Private;
503 property.Type = new CodeTypeReference (typeof (int));
504 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
505 typeof (object), "value1");
506 property.Parameters.Add (param);
507 TypeDeclaration.Members.Add (property);
509 return GenerateCodeFromType (TypeDeclaration, options);
512 protected string GeneratePropertyOverloads3 (CodeGeneratorOptions options)
514 TypeDeclaration.Name = "Test1";
516 CodeMemberProperty property = new CodeMemberProperty ();
517 property.Name = "Name";
518 property.Attributes = MemberAttributes.Public;
519 property.Type = new CodeTypeReference (typeof (int));
520 TypeDeclaration.Members.Add (property);
522 property = new CodeMemberProperty ();
523 property.Name = "Name";
524 property.Attributes = MemberAttributes.Private;
525 property.Type = new CodeTypeReference (typeof (int));
526 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
527 typeof (object), "value1");
528 property.Parameters.Add (param);
529 property.PrivateImplementationType = new CodeTypeReference (typeof (int));
530 TypeDeclaration.Members.Add (property);
532 return GenerateCodeFromType (TypeDeclaration, options);
535 protected string GeneratePropertyPrivateImplementationType (CodeGeneratorOptions options)
537 TypeDeclaration.Name = "Test1";
539 CodeMemberProperty property = new CodeMemberProperty ();
540 property.Name = "Item";
541 property.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
542 property.Type = new CodeTypeReference (typeof (int));
543 property.PrivateImplementationType = new CodeTypeReference (typeof (int));
544 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
545 typeof (object), "value1");
546 property.Parameters.Add (param);
547 TypeDeclaration.Members.Add (property);
549 return GenerateCodeFromType (TypeDeclaration, options);
552 protected string GeneratePropertyImplementationTypeOrder (CodeGeneratorOptions options)
554 TypeDeclaration.Name = "Test1";
556 CodeMemberProperty property = new CodeMemberProperty ();
557 property.Name = "Item";
558 property.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
559 property.Type = new CodeTypeReference (typeof (int));
560 property.PrivateImplementationType = new CodeTypeReference (typeof (int));
561 property.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
562 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
563 typeof (object), "value1");
564 property.Parameters.Add (param);
565 TypeDeclaration.Members.Add (property);
567 return GenerateCodeFromType (TypeDeclaration, options);
570 protected string GenerateMethodMembersType1 (CodeGeneratorOptions options)
572 TypeDeclaration.Name = "Test1";
574 CodeMemberMethod method = new CodeMemberMethod ();
576 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
578 method.CustomAttributes.Add (attrDec);
580 attrDec = new CodeAttributeDeclaration ();
582 method.CustomAttributes.Add (attrDec);
584 TypeDeclaration.Members.Add (method);
586 return GenerateCodeFromType (TypeDeclaration, options);
589 protected string GenerateMethodMembersType2 (CodeGeneratorOptions options)
591 TypeDeclaration.Name = "Test1";
593 CodeMemberMethod method = new CodeMemberMethod ();
594 method.Name = "Something";
595 method.Attributes = MemberAttributes.Public;
596 method.ReturnType = new CodeTypeReference (typeof (int));
598 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
599 typeof (object), "value1");
600 method.Parameters.Add (param);
602 param = new CodeParameterDeclarationExpression (
603 typeof (object), "value2");
604 param.Direction = FieldDirection.In;
605 method.Parameters.Add (param);
607 param = new CodeParameterDeclarationExpression (typeof (int), "index");
608 param.Direction = FieldDirection.Out;
609 method.Parameters.Add (param);
611 param = new CodeParameterDeclarationExpression (typeof (int), "count");
612 param.Direction = FieldDirection.Ref;
613 method.Parameters.Add (param);
615 TypeDeclaration.Members.Add (method);
617 return GenerateCodeFromType (TypeDeclaration, options);
620 protected string GenerateMethodMembersType3 (CodeGeneratorOptions options)
623 TypeDeclaration.Name = "Test1";
625 CodeMemberMethod method = new CodeMemberMethod ();
626 method.Name = "Something";
627 method.Attributes = MemberAttributes.Public;
628 method.ReturnType = new CodeTypeReference (typeof (int));
631 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
632 typeof (object), "value");
633 method.Parameters.Add (param);
635 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
637 param.CustomAttributes.Add (attrDec);
639 attrDec = new CodeAttributeDeclaration ();
641 param.CustomAttributes.Add (attrDec);
644 param = new CodeParameterDeclarationExpression (typeof (int), null);
645 param.Direction = FieldDirection.Out;
646 method.Parameters.Add (param);
648 attrDec = new CodeAttributeDeclaration ();
650 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
651 new CodePrimitiveExpression (false)));
652 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
653 new CodePrimitiveExpression (true)));
654 param.CustomAttributes.Add (attrDec);
656 attrDec = new CodeAttributeDeclaration ();
658 param.CustomAttributes.Add (attrDec);
660 TypeDeclaration.Members.Add (method);
662 return GenerateCodeFromType (TypeDeclaration, options);
665 protected string GenerateMethodImplementationTypes (CodeGeneratorOptions options)
667 TypeDeclaration.Name = "Test1";
669 CodeMemberMethod method = new CodeMemberMethod ();
670 method.Name = "Execute";
671 method.Attributes = MemberAttributes.Assembly;
672 method.ReturnType = new CodeTypeReference (typeof (int));
673 method.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
674 method.ImplementationTypes.Add (new CodeTypeReference ("IWhatever"));
675 TypeDeclaration.Members.Add (method);
677 return GenerateCodeFromType (TypeDeclaration, options);
680 protected string GenerateMethodOverloads1 (CodeGeneratorOptions options)
682 TypeDeclaration.Name = "Test1";
684 CodeMemberMethod method = new CodeMemberMethod ();
685 method.Name = "Execute";
686 method.Attributes = MemberAttributes.Assembly | MemberAttributes.Overloaded;
687 method.ReturnType = new CodeTypeReference (typeof (int));
688 TypeDeclaration.Members.Add (method);
690 return GenerateCodeFromType (TypeDeclaration, options);
693 protected string GenerateMethodOverloads2 (CodeGeneratorOptions options)
695 TypeDeclaration.Name = "Test1";
697 CodeMemberMethod method = new CodeMemberMethod ();
698 method.Name = "Execute";
699 method.Attributes = MemberAttributes.Public;
700 TypeDeclaration.Members.Add (method);
702 method = new CodeMemberMethod ();
703 method.Name = "Execute";
704 method.Attributes = MemberAttributes.Private;
705 method.ReturnType = new CodeTypeReference (typeof (int));
706 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
707 typeof (object), "value1");
708 method.Parameters.Add (param);
709 TypeDeclaration.Members.Add (method);
711 return GenerateCodeFromType (TypeDeclaration, options);
714 protected string GenerateMethodOverloads3 (CodeGeneratorOptions options)
716 TypeDeclaration.Name = "Test1";
718 CodeMemberMethod method = new CodeMemberMethod ();
719 method.Name = "Execute";
720 method.Attributes = MemberAttributes.Public;
721 TypeDeclaration.Members.Add (method);
723 method = new CodeMemberMethod ();
724 method.Name = "Execute";
725 method.Attributes = MemberAttributes.Private;
726 method.ReturnType = new CodeTypeReference (typeof (int));
727 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
728 typeof (object), "value1");
729 method.Parameters.Add (param);
730 method.PrivateImplementationType = new CodeTypeReference (typeof (int));
731 TypeDeclaration.Members.Add (method);
733 return GenerateCodeFromType (TypeDeclaration, options);
736 protected string GenerateMethodPrivateImplementationType (CodeGeneratorOptions options)
738 TypeDeclaration.Name = "Test1";
740 CodeMemberMethod method = new CodeMemberMethod ();
741 method.Name = "Execute";
742 method.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
743 method.ReturnType = new CodeTypeReference (typeof (int));
744 method.PrivateImplementationType = new CodeTypeReference (typeof (int));
745 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
746 typeof (object), "value1");
747 method.Parameters.Add (param);
748 TypeDeclaration.Members.Add (method);
750 return GenerateCodeFromType (TypeDeclaration, options);
753 protected string GenerateMethodImplementationTypeOrder (CodeGeneratorOptions options)
755 TypeDeclaration.Name = "Test1";
757 CodeMemberMethod method = new CodeMemberMethod ();
758 method.Name = "Execute";
759 method.Attributes = MemberAttributes.Public;
760 method.ReturnType = new CodeTypeReference (typeof (int));
761 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
762 typeof (object), "value1");
763 method.Parameters.Add (param);
764 method.PrivateImplementationType = new CodeTypeReference (typeof (int));
765 method.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
766 TypeDeclaration.Members.Add (method);
768 return GenerateCodeFromType (TypeDeclaration, options);
771 protected string GenerateMethodParamArrayAttribute (CodeGeneratorOptions options)
773 TypeDeclaration.Name = "Test1";
775 CodeMemberMethod method = new CodeMemberMethod ();
776 method.Name = "Something";
777 method.Attributes = MemberAttributes.Public;
778 method.ReturnType = new CodeTypeReference (typeof (int));
781 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
782 typeof (object), "value");
783 param.Direction = FieldDirection.Out;
784 method.Parameters.Add (param);
786 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
788 param.CustomAttributes.Add (attrDec);
790 attrDec = new CodeAttributeDeclaration ();
791 attrDec.Name = typeof (ParamArrayAttribute).FullName;
792 param.CustomAttributes.Add (attrDec);
794 attrDec = new CodeAttributeDeclaration ();
796 param.CustomAttributes.Add (attrDec);
799 param = new CodeParameterDeclarationExpression (typeof (int), null);
800 param.Direction = FieldDirection.Ref;
801 method.Parameters.Add (param);
803 attrDec = new CodeAttributeDeclaration ();
805 param.CustomAttributes.Add (attrDec);
807 TypeDeclaration.Members.Add (method);
809 return GenerateCodeFromType (TypeDeclaration, options);
812 protected string GenerateMethodReturnTypeAttributes (CodeGeneratorOptions options)
814 TypeDeclaration.Name = "Test1";
816 CodeMemberMethod method = new CodeMemberMethod ();
817 method.Name = "Execute";
818 method.Attributes = MemberAttributes.Public;
819 method.ReturnType = new CodeTypeReference (typeof (int));
820 TypeDeclaration.Members.Add (method);
822 // method custom attributes
823 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
825 method.CustomAttributes.Add (attrDec);
827 attrDec = new CodeAttributeDeclaration ();
829 method.CustomAttributes.Add (attrDec);
831 attrDec = new CodeAttributeDeclaration ();
832 attrDec.Name = typeof (ParamArrayAttribute).FullName;
833 method.CustomAttributes.Add (attrDec);
835 // return TypeDeclaration custom attributes
836 attrDec = new CodeAttributeDeclaration ();
838 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
839 new CodePrimitiveExpression (false)));
840 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
841 new CodePrimitiveExpression (true)));
842 method.ReturnTypeCustomAttributes.Add (attrDec);
844 attrDec = new CodeAttributeDeclaration ();
845 attrDec.Name = typeof (ParamArrayAttribute).FullName;
846 method.ReturnTypeCustomAttributes.Add (attrDec);
848 attrDec = new CodeAttributeDeclaration ();
850 method.ReturnTypeCustomAttributes.Add (attrDec);
852 return GenerateCodeFromType (TypeDeclaration, options);
855 protected string GenerateConstructorAttributes (CodeGeneratorOptions options)
857 TypeDeclaration.Name = "Test1";
859 CodeConstructor ctor = new CodeConstructor ();
861 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
863 ctor.CustomAttributes.Add (attrDec);
865 attrDec = new CodeAttributeDeclaration ();
867 ctor.CustomAttributes.Add (attrDec);
869 TypeDeclaration.Members.Add (ctor);
871 return GenerateCodeFromType (TypeDeclaration, options);
874 protected string GenerateMethodNewSlot (CodeGeneratorOptions options)
876 TypeDeclaration.Name = "Test1";
878 CodeMemberMethod method = new CodeMemberMethod ();
879 method.Name = "Execute";
880 method.Attributes = MemberAttributes.Public | MemberAttributes.New;
881 method.ReturnType = new CodeTypeReference (typeof (int));
882 TypeDeclaration.Members.Add (method);
884 return GenerateCodeFromType (TypeDeclaration, options);
887 protected string GenerateConstructorParameters (CodeGeneratorOptions options)
889 TypeDeclaration.Name = "Test1";
891 CodeConstructor ctor = new CodeConstructor ();
892 ctor.Name = "Whatever";
893 ctor.Attributes = MemberAttributes.Public;
895 // scope and vtable modifiers should be ignored
896 ctor.Attributes |= MemberAttributes.Abstract | MemberAttributes.Const
897 | MemberAttributes.Final | MemberAttributes.New
898 | MemberAttributes.Overloaded | MemberAttributes.Override
899 | MemberAttributes.Static;
901 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
902 typeof (object), "value1");
903 ctor.Parameters.Add (param);
905 param = new CodeParameterDeclarationExpression (
906 typeof (object), "value2");
907 param.Direction = FieldDirection.In;
908 ctor.Parameters.Add (param);
910 param = new CodeParameterDeclarationExpression (typeof (int), "index");
911 param.Direction = FieldDirection.Out;
912 ctor.Parameters.Add (param);
914 param = new CodeParameterDeclarationExpression (typeof (int), "count");
915 param.Direction = FieldDirection.Ref;
916 ctor.Parameters.Add (param);
918 TypeDeclaration.Members.Add (ctor);
920 return GenerateCodeFromType (TypeDeclaration, options);
923 protected string GenerateConstructorParameterAttributes (CodeGeneratorOptions options)
925 TypeDeclaration.Name = "Test1";
927 CodeConstructor ctor = new CodeConstructor ();
928 ctor.Name = "Something";
929 ctor.Attributes = MemberAttributes.Private;
932 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
933 typeof (object), "value");
934 ctor.Parameters.Add (param);
936 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
938 param.CustomAttributes.Add (attrDec);
940 attrDec = new CodeAttributeDeclaration ();
942 param.CustomAttributes.Add (attrDec);
945 param = new CodeParameterDeclarationExpression (typeof (int), "index");
946 param.Direction = FieldDirection.Out;
947 ctor.Parameters.Add (param);
949 attrDec = new CodeAttributeDeclaration ();
951 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
952 new CodePrimitiveExpression (false)));
953 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
954 new CodePrimitiveExpression (true)));
955 param.CustomAttributes.Add (attrDec);
957 attrDec = new CodeAttributeDeclaration ();
959 param.CustomAttributes.Add (attrDec);
961 TypeDeclaration.Members.Add (ctor);
963 return GenerateCodeFromType (TypeDeclaration, options);
966 protected string GenerateBaseConstructor (bool multipleArgs, CodeGeneratorOptions options)
968 TypeDeclaration.Name = "Test1";
970 CodeConstructor ctor = new CodeConstructor ();
971 ctor.Name = "Something";
972 ctor.Attributes = MemberAttributes.Family;
975 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
976 typeof (object), "value1");
977 ctor.Parameters.Add (param);
980 param = new CodeParameterDeclarationExpression (typeof (int), "value2");
981 param.Direction = FieldDirection.Out;
982 ctor.Parameters.Add (param);
985 ctor.BaseConstructorArgs.Add (new CodeVariableReferenceExpression ("value1"));
988 ctor.BaseConstructorArgs.Add (new CodeVariableReferenceExpression ("value2"));
991 TypeDeclaration.Members.Add (ctor);
993 return GenerateCodeFromType (TypeDeclaration, options);
996 protected string GenerateChainedConstructor (bool multipleArgs, CodeGeneratorOptions options)
998 TypeDeclaration.Name = "Test1";
1000 CodeConstructor ctor = new CodeConstructor ();
1001 ctor.Name = "Something";
1002 ctor.Attributes = MemberAttributes.Public;
1005 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
1006 typeof (object), "value1");
1007 ctor.Parameters.Add (param);
1010 param = new CodeParameterDeclarationExpression (typeof (int), "value2");
1011 param.Direction = FieldDirection.Out;
1012 ctor.Parameters.Add (param);
1014 // implementation types should be ignored on ctors
1015 ctor.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
1017 // chained ctor args
1018 ctor.ChainedConstructorArgs.Add (new CodeVariableReferenceExpression ("value1"));
1021 ctor.ChainedConstructorArgs.Add (new CodeVariableReferenceExpression ("value2"));
1024 // should be ignored as chained ctor args should take precedence over base
1026 ctor.BaseConstructorArgs.Add (new CodeVariableReferenceExpression ("value3"));
1028 TypeDeclaration.Members.Add (ctor);
1030 return GenerateCodeFromType (TypeDeclaration, options);
1033 protected string GenerateTypeConstructor (CodeGeneratorOptions options)
1035 TypeDeclaration.Name = "Test1";
1037 CodeTypeConstructor typeCtor = new CodeTypeConstructor ();
1038 // access, scope and vtable modifiers should be ignored
1039 typeCtor.Attributes |= MemberAttributes.Public | MemberAttributes.Abstract
1040 | MemberAttributes.Const | MemberAttributes.Final
1041 | MemberAttributes.New | MemberAttributes.Overloaded
1042 | MemberAttributes.Override | MemberAttributes.Static;
1043 TypeDeclaration.Members.Add (typeCtor);
1045 // custom attributes
1046 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
1048 typeCtor.CustomAttributes.Add (attrDec);
1050 attrDec = new CodeAttributeDeclaration ();
1052 typeCtor.CustomAttributes.Add (attrDec);
1054 // parameter should be ignored
1055 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
1056 typeof (object), "value1");
1057 typeCtor.Parameters.Add (param);
1059 // implementation types should be ignored on type ctors
1060 typeCtor.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
1062 // private immplementation type should be ignored on type ctors
1063 typeCtor.PrivateImplementationType = new CodeTypeReference (typeof (int));
1065 // return type should be ignored on type ctors
1066 typeCtor.ReturnType = new CodeTypeReference (typeof (int));
1068 // return TypeDeclaration custom attributes
1069 attrDec = new CodeAttributeDeclaration ();
1071 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
1072 new CodePrimitiveExpression (false)));
1073 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
1074 new CodePrimitiveExpression (true)));
1075 typeCtor.ReturnTypeCustomAttributes.Add (attrDec);
1077 return GenerateCodeFromType (TypeDeclaration, options);
1080 protected string GenerateEntryPointMethod (CodeGeneratorOptions options)
1082 TypeDeclaration.Name = "Test1";
1084 CodeEntryPointMethod method = new CodeEntryPointMethod ();
1085 method.Name = "Something"; // should be ignored in C# and VB
1086 method.Attributes = MemberAttributes.Private ; // should be ignored in C# and VB
1087 method.ReturnType = new CodeTypeReference (typeof (int)); // should be ignored in C# 1.x and VB 1.x and 2.x
1089 // parameters on entry point are ignored in C# and VB
1090 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
1091 typeof (object), "value1");
1092 method.Parameters.Add (param);
1094 // custom attributes on entry point are ignored in C# 1.x and VB 1.x
1095 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
1097 method.CustomAttributes.Add (attrDec);
1099 CodeVariableDeclarationStatement v = new CodeVariableDeclarationStatement ("Test+InnerType", "x");
1100 method.Statements.Add (v);
1102 TypeDeclaration.Members.Add (method);
1105 CodeTypeDeclaration nestedType = new CodeTypeDeclaration ("InnerType");
1106 TypeDeclaration.Members.Add (nestedType);
1109 return GenerateCodeFromType (TypeDeclaration, options);
1113 protected string GenerateGenericCodeTypeReferences (CodeGeneratorOptions options)
1115 CodeTypeDeclaration td = new CodeTypeDeclaration ("Test");
1116 CodeMemberField f = new CodeMemberField (
1117 new CodeTypeReference ("System.Nullable",
1118 new CodeTypeReference (typeof (int))),
1121 CodeMemberField f2 = new CodeMemberField (
1122 new CodeTypeReference (typeof (int?).GetGenericTypeDefinition ()),
1124 td.Members.Add (f2);
1125 return GenerateCodeFromType (td, options);
1128 protected string GeneratePartialType (CodeGeneratorOptions options)
1130 TypeDeclaration.Name = "Test1";
1131 TypeDeclaration.IsPartial = true;
1132 return GenerateCodeFromType (TypeDeclaration, options);