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 ();
188 public abstract void PartialTypeTest ();
190 protected string GenerateDefaultType (CodeGeneratorOptions options)
192 return GenerateCodeFromType (TypeDeclaration, options);
195 protected string GenerateNullType (CodeGeneratorOptions options)
197 return GenerateCodeFromType (null, options);
200 protected string GenerateSimpleType (CodeGeneratorOptions options)
202 TypeDeclaration.Name = "Test1";
203 return GenerateCodeFromType (TypeDeclaration, options);
206 protected string GenerateDerivedType (CodeGeneratorOptions options)
208 TypeDeclaration.Name = "Test1";
209 TypeDeclaration.TypeAttributes |= TypeAttributes.NestedFamily |
210 TypeAttributes.Abstract;
211 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (int)));
212 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (IIdentity)));
213 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (string)));
214 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (IPermission)));
215 return GenerateCodeFromType (TypeDeclaration, options);
218 protected string GenerateAttributesAndType (CodeGeneratorOptions options)
220 TypeDeclaration.Name = "Test1";
222 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
224 TypeDeclaration.CustomAttributes.Add (attrDec);
226 attrDec = new CodeAttributeDeclaration ();
228 TypeDeclaration.CustomAttributes.Add (attrDec);
230 return GenerateCodeFromType (TypeDeclaration, options);
233 protected string GenerateEventMembersType1 (CodeGeneratorOptions options)
235 TypeDeclaration.Name = "Test1";
237 CodeMemberEvent evt = new CodeMemberEvent ();
239 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
241 evt.CustomAttributes.Add (attrDec);
243 attrDec = new CodeAttributeDeclaration ();
245 evt.CustomAttributes.Add (attrDec);
247 TypeDeclaration.Members.Add (evt);
249 return GenerateCodeFromType (TypeDeclaration, options);
252 protected string GenerateEventMembersType2 (CodeGeneratorOptions options)
254 TypeDeclaration.Name = "Test1";
256 CodeMemberEvent evt = new CodeMemberEvent ();
258 evt.Attributes = MemberAttributes.Public | MemberAttributes.Override
259 | MemberAttributes.Static | MemberAttributes.Abstract |
260 MemberAttributes.New;
261 evt.Type = new CodeTypeReference (typeof (int));
262 TypeDeclaration.Members.Add (evt);
264 return GenerateCodeFromType (TypeDeclaration, options);
267 protected string GenerateEventImplementationTypes (CodeGeneratorOptions options)
269 TypeDeclaration.Name = "Test1";
271 CodeMemberEvent evt = new CodeMemberEvent ();
273 evt.Attributes = MemberAttributes.FamilyAndAssembly;
274 evt.Type = new CodeTypeReference (typeof (int));
275 evt.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
276 evt.ImplementationTypes.Add (new CodeTypeReference ("IWhatever"));
277 TypeDeclaration.Members.Add (evt);
279 return GenerateCodeFromType (TypeDeclaration, options);
282 protected string GenerateEventPrivateImplementationType (CodeGeneratorOptions options)
284 TypeDeclaration.Name = "Test1";
286 CodeMemberEvent evt = new CodeMemberEvent ();
288 evt.Attributes = MemberAttributes.Family | MemberAttributes.Overloaded;
289 evt.Type = new CodeTypeReference (typeof (int));
290 evt.PrivateImplementationType = new CodeTypeReference (typeof (int));
291 TypeDeclaration.Members.Add (evt);
293 return GenerateCodeFromType (TypeDeclaration, options);
296 protected string GenerateEventImplementationTypeOrder (CodeGeneratorOptions options)
298 TypeDeclaration.Name = "Test1";
300 CodeMemberEvent evt = new CodeMemberEvent ();
302 evt.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
303 evt.Type = new CodeTypeReference (typeof (int));
304 evt.PrivateImplementationType = new CodeTypeReference (typeof (int));
305 evt.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
306 TypeDeclaration.Members.Add (evt);
308 return GenerateCodeFromType (TypeDeclaration, options);
311 protected string GenerateFieldMembersAttributes (CodeGeneratorOptions options)
313 TypeDeclaration.Name = "Test1";
315 CodeMemberField fld = new CodeMemberField ();
317 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
319 fld.CustomAttributes.Add (attrDec);
321 attrDec = new CodeAttributeDeclaration ();
323 fld.CustomAttributes.Add (attrDec);
325 TypeDeclaration.Members.Add (fld);
327 return GenerateCodeFromType (TypeDeclaration, options);
330 protected string GenerateFieldMembersType (MemberAttributes memberAttributes, CodeGeneratorOptions options)
332 TypeDeclaration.Name = "Test1";
334 CodeMemberField fld = new CodeMemberField ();
336 fld.Attributes = memberAttributes;
337 fld.Type = new CodeTypeReference (typeof (int));
338 fld.InitExpression = new CodePrimitiveExpression (2);
339 TypeDeclaration.Members.Add (fld);
341 return GenerateCodeFromType (TypeDeclaration, options);
344 protected string GeneratePropertyMembersAttributes (CodeGeneratorOptions options)
346 TypeDeclaration.Name = "Test1";
348 CodeMemberProperty property = new CodeMemberProperty ();
349 TypeDeclaration.Members.Add (property);
351 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
353 property.CustomAttributes.Add (attrDec);
355 attrDec = new CodeAttributeDeclaration ();
357 property.CustomAttributes.Add (attrDec);
359 return GenerateCodeFromType (TypeDeclaration, options);
362 protected string GenerateAbstractProperty (CodeGeneratorOptions options)
364 TypeDeclaration.Name = "Test1";
365 TypeDeclaration.TypeAttributes = TypeAttributes.Abstract | TypeAttributes.Public;
367 CodeMemberProperty property = new CodeMemberProperty ();
368 property.Name = "Name";
369 property.Attributes = MemberAttributes.Public | MemberAttributes.Abstract;
370 property.HasGet = true;
371 property.HasSet = true;
372 property.Type = new CodeTypeReference (typeof (string));
374 TypeDeclaration.Members.Add (property);
376 return GenerateCodeFromType (TypeDeclaration, options);
379 protected string GenerateStaticProperty (CodeGeneratorOptions options)
381 TypeDeclaration.Name = "Test1";
382 TypeDeclaration.TypeAttributes = TypeAttributes.Public;
384 CodeMemberProperty property = new CodeMemberProperty ();
385 property.Name = "Name";
386 property.Attributes = MemberAttributes.Public | MemberAttributes.Static;
387 property.HasSet = true;
388 property.Type = new CodeTypeReference (typeof (string));
390 TypeDeclaration.Members.Add (property);
392 return GenerateCodeFromType (TypeDeclaration, options);
395 protected string GeneratePropertyMembersType (MemberAttributes memberAttributes, bool hasGet, bool hasSet, CodeGeneratorOptions options)
397 TypeDeclaration.Name = "Test1";
399 CodeMemberProperty property = new CodeMemberProperty ();
400 property.Name = "Name";
401 property.Attributes = memberAttributes;
402 property.HasGet = hasGet;
403 property.HasSet = hasSet;
404 property.Type = new CodeTypeReference (typeof (int));
405 TypeDeclaration.Members.Add (property);
407 return GenerateCodeFromType (TypeDeclaration, options);
410 protected string GeneratePropertyParameters (CodeGeneratorOptions options)
412 TypeDeclaration.Name = "Test1";
414 CodeMemberProperty property = new CodeMemberProperty ();
415 property.Name = "Name";
416 property.Attributes = MemberAttributes.Public;
417 property.Type = new CodeTypeReference (typeof (int));
419 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
420 typeof (object), "value1");
421 property.Parameters.Add (param);
423 param = new CodeParameterDeclarationExpression (
424 typeof (int), "value2");
425 param.Direction = FieldDirection.Ref;
426 property.Parameters.Add (param);
428 TypeDeclaration.Members.Add (property);
430 return GenerateCodeFromType (TypeDeclaration, options);
433 protected string GeneratePropertyIndexer (MemberAttributes memberAttributes, bool hasGet, bool hasSet, bool addParameters, CodeGeneratorOptions options)
435 TypeDeclaration.Name = "Test1";
437 CodeMemberProperty property = new CodeMemberProperty ();
438 // ensure case-insensitive comparison is done on name of property
439 property.Name = "iTem";
440 property.Attributes = memberAttributes;
441 property.HasGet = hasGet;
442 property.HasSet = hasSet;
443 property.Type = new CodeTypeReference (typeof (int));
444 TypeDeclaration.Members.Add (property);
447 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
448 typeof (object), "value1");
449 property.Parameters.Add (param);
451 param = new CodeParameterDeclarationExpression (
452 typeof (int), "value2");
453 param.Direction = FieldDirection.Ref;
454 property.Parameters.Add (param);
457 return GenerateCodeFromType (TypeDeclaration, options);
460 protected string GeneratePropertyImplementationTypes (CodeGeneratorOptions options)
462 TypeDeclaration.Name = "Test1";
464 CodeMemberProperty property = new CodeMemberProperty ();
465 property.Name = "Name";
466 property.Attributes = MemberAttributes.Public;
467 property.Type = new CodeTypeReference (typeof (int));
468 property.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
469 property.ImplementationTypes.Add (new CodeTypeReference ("IWhatever"));
470 TypeDeclaration.Members.Add (property);
472 return GenerateCodeFromType (TypeDeclaration, options);
475 protected string GeneratePropertyOverloads1 (CodeGeneratorOptions options)
477 TypeDeclaration.Name = "Test1";
479 CodeMemberProperty property = new CodeMemberProperty ();
480 property.Name = "Name";
481 property.Attributes = MemberAttributes.Family | MemberAttributes.Overloaded;
482 property.Type = new CodeTypeReference (typeof (int));
483 TypeDeclaration.Members.Add (property);
485 return GenerateCodeFromType (TypeDeclaration, options);
488 protected string GeneratePropertyOverloads2 (CodeGeneratorOptions options)
490 TypeDeclaration.Name = "Test1";
492 CodeMemberProperty property = new CodeMemberProperty ();
493 property.Name = "Name";
494 property.Attributes = MemberAttributes.Public;
495 property.Type = new CodeTypeReference (typeof (int));
496 TypeDeclaration.Members.Add (property);
498 property = new CodeMemberProperty ();
499 property.Name = "Name";
500 property.Attributes = MemberAttributes.Private;
501 property.Type = new CodeTypeReference (typeof (int));
502 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
503 typeof (object), "value1");
504 property.Parameters.Add (param);
505 TypeDeclaration.Members.Add (property);
507 return GenerateCodeFromType (TypeDeclaration, options);
510 protected string GeneratePropertyOverloads3 (CodeGeneratorOptions options)
512 TypeDeclaration.Name = "Test1";
514 CodeMemberProperty property = new CodeMemberProperty ();
515 property.Name = "Name";
516 property.Attributes = MemberAttributes.Public;
517 property.Type = new CodeTypeReference (typeof (int));
518 TypeDeclaration.Members.Add (property);
520 property = new CodeMemberProperty ();
521 property.Name = "Name";
522 property.Attributes = MemberAttributes.Private;
523 property.Type = new CodeTypeReference (typeof (int));
524 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
525 typeof (object), "value1");
526 property.Parameters.Add (param);
527 property.PrivateImplementationType = new CodeTypeReference (typeof (int));
528 TypeDeclaration.Members.Add (property);
530 return GenerateCodeFromType (TypeDeclaration, options);
533 protected string GeneratePropertyPrivateImplementationType (CodeGeneratorOptions options)
535 TypeDeclaration.Name = "Test1";
537 CodeMemberProperty property = new CodeMemberProperty ();
538 property.Name = "Item";
539 property.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
540 property.Type = new CodeTypeReference (typeof (int));
541 property.PrivateImplementationType = new CodeTypeReference (typeof (int));
542 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
543 typeof (object), "value1");
544 property.Parameters.Add (param);
545 TypeDeclaration.Members.Add (property);
547 return GenerateCodeFromType (TypeDeclaration, options);
550 protected string GeneratePropertyImplementationTypeOrder (CodeGeneratorOptions options)
552 TypeDeclaration.Name = "Test1";
554 CodeMemberProperty property = new CodeMemberProperty ();
555 property.Name = "Item";
556 property.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
557 property.Type = new CodeTypeReference (typeof (int));
558 property.PrivateImplementationType = new CodeTypeReference (typeof (int));
559 property.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
560 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
561 typeof (object), "value1");
562 property.Parameters.Add (param);
563 TypeDeclaration.Members.Add (property);
565 return GenerateCodeFromType (TypeDeclaration, options);
568 protected string GenerateMethodMembersType1 (CodeGeneratorOptions options)
570 TypeDeclaration.Name = "Test1";
572 CodeMemberMethod method = new CodeMemberMethod ();
574 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
576 method.CustomAttributes.Add (attrDec);
578 attrDec = new CodeAttributeDeclaration ();
580 method.CustomAttributes.Add (attrDec);
582 TypeDeclaration.Members.Add (method);
584 return GenerateCodeFromType (TypeDeclaration, options);
587 protected string GenerateMethodMembersType2 (CodeGeneratorOptions options)
589 TypeDeclaration.Name = "Test1";
591 CodeMemberMethod method = new CodeMemberMethod ();
592 method.Name = "Something";
593 method.Attributes = MemberAttributes.Public;
594 method.ReturnType = new CodeTypeReference (typeof (int));
596 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
597 typeof (object), "value1");
598 method.Parameters.Add (param);
600 param = new CodeParameterDeclarationExpression (
601 typeof (object), "value2");
602 param.Direction = FieldDirection.In;
603 method.Parameters.Add (param);
605 param = new CodeParameterDeclarationExpression (typeof (int), "index");
606 param.Direction = FieldDirection.Out;
607 method.Parameters.Add (param);
609 param = new CodeParameterDeclarationExpression (typeof (int), "count");
610 param.Direction = FieldDirection.Ref;
611 method.Parameters.Add (param);
613 TypeDeclaration.Members.Add (method);
615 return GenerateCodeFromType (TypeDeclaration, options);
618 protected string GenerateMethodMembersType3 (CodeGeneratorOptions options)
621 TypeDeclaration.Name = "Test1";
623 CodeMemberMethod method = new CodeMemberMethod ();
624 method.Name = "Something";
625 method.Attributes = MemberAttributes.Public;
626 method.ReturnType = new CodeTypeReference (typeof (int));
629 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
630 typeof (object), "value");
631 method.Parameters.Add (param);
633 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
635 param.CustomAttributes.Add (attrDec);
637 attrDec = new CodeAttributeDeclaration ();
639 param.CustomAttributes.Add (attrDec);
642 param = new CodeParameterDeclarationExpression (typeof (int), null);
643 param.Direction = FieldDirection.Out;
644 method.Parameters.Add (param);
646 attrDec = new CodeAttributeDeclaration ();
648 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
649 new CodePrimitiveExpression (false)));
650 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
651 new CodePrimitiveExpression (true)));
652 param.CustomAttributes.Add (attrDec);
654 attrDec = new CodeAttributeDeclaration ();
656 param.CustomAttributes.Add (attrDec);
658 TypeDeclaration.Members.Add (method);
660 return GenerateCodeFromType (TypeDeclaration, options);
663 protected string GenerateMethodImplementationTypes (CodeGeneratorOptions options)
665 TypeDeclaration.Name = "Test1";
667 CodeMemberMethod method = new CodeMemberMethod ();
668 method.Name = "Execute";
669 method.Attributes = MemberAttributes.Assembly;
670 method.ReturnType = new CodeTypeReference (typeof (int));
671 method.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
672 method.ImplementationTypes.Add (new CodeTypeReference ("IWhatever"));
673 TypeDeclaration.Members.Add (method);
675 return GenerateCodeFromType (TypeDeclaration, options);
678 protected string GenerateMethodOverloads1 (CodeGeneratorOptions options)
680 TypeDeclaration.Name = "Test1";
682 CodeMemberMethod method = new CodeMemberMethod ();
683 method.Name = "Execute";
684 method.Attributes = MemberAttributes.Assembly | MemberAttributes.Overloaded;
685 method.ReturnType = new CodeTypeReference (typeof (int));
686 TypeDeclaration.Members.Add (method);
688 return GenerateCodeFromType (TypeDeclaration, options);
691 protected string GenerateMethodOverloads2 (CodeGeneratorOptions options)
693 TypeDeclaration.Name = "Test1";
695 CodeMemberMethod method = new CodeMemberMethod ();
696 method.Name = "Execute";
697 method.Attributes = MemberAttributes.Public;
698 TypeDeclaration.Members.Add (method);
700 method = new CodeMemberMethod ();
701 method.Name = "Execute";
702 method.Attributes = MemberAttributes.Private;
703 method.ReturnType = new CodeTypeReference (typeof (int));
704 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
705 typeof (object), "value1");
706 method.Parameters.Add (param);
707 TypeDeclaration.Members.Add (method);
709 return GenerateCodeFromType (TypeDeclaration, options);
712 protected string GenerateMethodOverloads3 (CodeGeneratorOptions options)
714 TypeDeclaration.Name = "Test1";
716 CodeMemberMethod method = new CodeMemberMethod ();
717 method.Name = "Execute";
718 method.Attributes = MemberAttributes.Public;
719 TypeDeclaration.Members.Add (method);
721 method = new CodeMemberMethod ();
722 method.Name = "Execute";
723 method.Attributes = MemberAttributes.Private;
724 method.ReturnType = new CodeTypeReference (typeof (int));
725 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
726 typeof (object), "value1");
727 method.Parameters.Add (param);
728 method.PrivateImplementationType = new CodeTypeReference (typeof (int));
729 TypeDeclaration.Members.Add (method);
731 return GenerateCodeFromType (TypeDeclaration, options);
734 protected string GenerateMethodPrivateImplementationType (CodeGeneratorOptions options)
736 TypeDeclaration.Name = "Test1";
738 CodeMemberMethod method = new CodeMemberMethod ();
739 method.Name = "Execute";
740 method.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
741 method.ReturnType = new CodeTypeReference (typeof (int));
742 method.PrivateImplementationType = new CodeTypeReference (typeof (int));
743 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
744 typeof (object), "value1");
745 method.Parameters.Add (param);
746 TypeDeclaration.Members.Add (method);
748 return GenerateCodeFromType (TypeDeclaration, options);
751 protected string GenerateMethodImplementationTypeOrder (CodeGeneratorOptions options)
753 TypeDeclaration.Name = "Test1";
755 CodeMemberMethod method = new CodeMemberMethod ();
756 method.Name = "Execute";
757 method.Attributes = MemberAttributes.Public;
758 method.ReturnType = new CodeTypeReference (typeof (int));
759 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
760 typeof (object), "value1");
761 method.Parameters.Add (param);
762 method.PrivateImplementationType = new CodeTypeReference (typeof (int));
763 method.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
764 TypeDeclaration.Members.Add (method);
766 return GenerateCodeFromType (TypeDeclaration, options);
769 protected string GenerateMethodParamArrayAttribute (CodeGeneratorOptions options)
771 TypeDeclaration.Name = "Test1";
773 CodeMemberMethod method = new CodeMemberMethod ();
774 method.Name = "Something";
775 method.Attributes = MemberAttributes.Public;
776 method.ReturnType = new CodeTypeReference (typeof (int));
779 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
780 typeof (object), "value");
781 param.Direction = FieldDirection.Out;
782 method.Parameters.Add (param);
784 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
786 param.CustomAttributes.Add (attrDec);
788 attrDec = new CodeAttributeDeclaration ();
789 attrDec.Name = typeof (ParamArrayAttribute).FullName;
790 param.CustomAttributes.Add (attrDec);
792 attrDec = new CodeAttributeDeclaration ();
794 param.CustomAttributes.Add (attrDec);
797 param = new CodeParameterDeclarationExpression (typeof (int), null);
798 param.Direction = FieldDirection.Ref;
799 method.Parameters.Add (param);
801 attrDec = new CodeAttributeDeclaration ();
803 param.CustomAttributes.Add (attrDec);
805 TypeDeclaration.Members.Add (method);
807 return GenerateCodeFromType (TypeDeclaration, options);
810 protected string GenerateMethodReturnTypeAttributes (CodeGeneratorOptions options)
812 TypeDeclaration.Name = "Test1";
814 CodeMemberMethod method = new CodeMemberMethod ();
815 method.Name = "Execute";
816 method.Attributes = MemberAttributes.Public;
817 method.ReturnType = new CodeTypeReference (typeof (int));
818 TypeDeclaration.Members.Add (method);
820 // method custom attributes
821 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
823 method.CustomAttributes.Add (attrDec);
825 attrDec = new CodeAttributeDeclaration ();
827 method.CustomAttributes.Add (attrDec);
829 attrDec = new CodeAttributeDeclaration ();
830 attrDec.Name = typeof (ParamArrayAttribute).FullName;
831 method.CustomAttributes.Add (attrDec);
833 // return TypeDeclaration custom attributes
834 attrDec = new CodeAttributeDeclaration ();
836 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
837 new CodePrimitiveExpression (false)));
838 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
839 new CodePrimitiveExpression (true)));
840 method.ReturnTypeCustomAttributes.Add (attrDec);
842 attrDec = new CodeAttributeDeclaration ();
843 attrDec.Name = typeof (ParamArrayAttribute).FullName;
844 method.ReturnTypeCustomAttributes.Add (attrDec);
846 attrDec = new CodeAttributeDeclaration ();
848 method.ReturnTypeCustomAttributes.Add (attrDec);
850 return GenerateCodeFromType (TypeDeclaration, options);
853 protected string GenerateConstructorAttributes (CodeGeneratorOptions options)
855 TypeDeclaration.Name = "Test1";
857 CodeConstructor ctor = new CodeConstructor ();
859 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
861 ctor.CustomAttributes.Add (attrDec);
863 attrDec = new CodeAttributeDeclaration ();
865 ctor.CustomAttributes.Add (attrDec);
867 TypeDeclaration.Members.Add (ctor);
869 return GenerateCodeFromType (TypeDeclaration, options);
872 protected string GenerateMethodNewSlot (CodeGeneratorOptions options)
874 TypeDeclaration.Name = "Test1";
876 CodeMemberMethod method = new CodeMemberMethod ();
877 method.Name = "Execute";
878 method.Attributes = MemberAttributes.Public | MemberAttributes.New;
879 method.ReturnType = new CodeTypeReference (typeof (int));
880 TypeDeclaration.Members.Add (method);
882 return GenerateCodeFromType (TypeDeclaration, options);
885 protected string GenerateConstructorParameters (CodeGeneratorOptions options)
887 TypeDeclaration.Name = "Test1";
889 CodeConstructor ctor = new CodeConstructor ();
890 ctor.Name = "Whatever";
891 ctor.Attributes = MemberAttributes.Public;
893 // scope and vtable modifiers should be ignored
894 ctor.Attributes |= MemberAttributes.Abstract | MemberAttributes.Const
895 | MemberAttributes.Final | MemberAttributes.New
896 | MemberAttributes.Overloaded | MemberAttributes.Override
897 | MemberAttributes.Static;
899 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
900 typeof (object), "value1");
901 ctor.Parameters.Add (param);
903 param = new CodeParameterDeclarationExpression (
904 typeof (object), "value2");
905 param.Direction = FieldDirection.In;
906 ctor.Parameters.Add (param);
908 param = new CodeParameterDeclarationExpression (typeof (int), "index");
909 param.Direction = FieldDirection.Out;
910 ctor.Parameters.Add (param);
912 param = new CodeParameterDeclarationExpression (typeof (int), "count");
913 param.Direction = FieldDirection.Ref;
914 ctor.Parameters.Add (param);
916 TypeDeclaration.Members.Add (ctor);
918 return GenerateCodeFromType (TypeDeclaration, options);
921 protected string GenerateConstructorParameterAttributes (CodeGeneratorOptions options)
923 TypeDeclaration.Name = "Test1";
925 CodeConstructor ctor = new CodeConstructor ();
926 ctor.Name = "Something";
927 ctor.Attributes = MemberAttributes.Private;
930 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
931 typeof (object), "value");
932 ctor.Parameters.Add (param);
934 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
936 param.CustomAttributes.Add (attrDec);
938 attrDec = new CodeAttributeDeclaration ();
940 param.CustomAttributes.Add (attrDec);
943 param = new CodeParameterDeclarationExpression (typeof (int), "index");
944 param.Direction = FieldDirection.Out;
945 ctor.Parameters.Add (param);
947 attrDec = new CodeAttributeDeclaration ();
949 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
950 new CodePrimitiveExpression (false)));
951 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
952 new CodePrimitiveExpression (true)));
953 param.CustomAttributes.Add (attrDec);
955 attrDec = new CodeAttributeDeclaration ();
957 param.CustomAttributes.Add (attrDec);
959 TypeDeclaration.Members.Add (ctor);
961 return GenerateCodeFromType (TypeDeclaration, options);
964 protected string GenerateBaseConstructor (bool multipleArgs, CodeGeneratorOptions options)
966 TypeDeclaration.Name = "Test1";
968 CodeConstructor ctor = new CodeConstructor ();
969 ctor.Name = "Something";
970 ctor.Attributes = MemberAttributes.Family;
973 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
974 typeof (object), "value1");
975 ctor.Parameters.Add (param);
978 param = new CodeParameterDeclarationExpression (typeof (int), "value2");
979 param.Direction = FieldDirection.Out;
980 ctor.Parameters.Add (param);
983 ctor.BaseConstructorArgs.Add (new CodeVariableReferenceExpression ("value1"));
986 ctor.BaseConstructorArgs.Add (new CodeVariableReferenceExpression ("value2"));
989 TypeDeclaration.Members.Add (ctor);
991 return GenerateCodeFromType (TypeDeclaration, options);
994 protected string GenerateChainedConstructor (bool multipleArgs, CodeGeneratorOptions options)
996 TypeDeclaration.Name = "Test1";
998 CodeConstructor ctor = new CodeConstructor ();
999 ctor.Name = "Something";
1000 ctor.Attributes = MemberAttributes.Public;
1003 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
1004 typeof (object), "value1");
1005 ctor.Parameters.Add (param);
1008 param = new CodeParameterDeclarationExpression (typeof (int), "value2");
1009 param.Direction = FieldDirection.Out;
1010 ctor.Parameters.Add (param);
1012 // implementation types should be ignored on ctors
1013 ctor.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
1015 // chained ctor args
1016 ctor.ChainedConstructorArgs.Add (new CodeVariableReferenceExpression ("value1"));
1019 ctor.ChainedConstructorArgs.Add (new CodeVariableReferenceExpression ("value2"));
1022 // should be ignored as chained ctor args should take precedence over base
1024 ctor.BaseConstructorArgs.Add (new CodeVariableReferenceExpression ("value3"));
1026 TypeDeclaration.Members.Add (ctor);
1028 return GenerateCodeFromType (TypeDeclaration, options);
1031 protected string GenerateTypeConstructor (CodeGeneratorOptions options)
1033 TypeDeclaration.Name = "Test1";
1035 CodeTypeConstructor typeCtor = new CodeTypeConstructor ();
1036 // access, scope and vtable modifiers should be ignored
1037 typeCtor.Attributes |= MemberAttributes.Public | MemberAttributes.Abstract
1038 | MemberAttributes.Const | MemberAttributes.Final
1039 | MemberAttributes.New | MemberAttributes.Overloaded
1040 | MemberAttributes.Override | MemberAttributes.Static;
1041 TypeDeclaration.Members.Add (typeCtor);
1043 // custom attributes
1044 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
1046 typeCtor.CustomAttributes.Add (attrDec);
1048 attrDec = new CodeAttributeDeclaration ();
1050 typeCtor.CustomAttributes.Add (attrDec);
1052 // parameter should be ignored
1053 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
1054 typeof (object), "value1");
1055 typeCtor.Parameters.Add (param);
1057 // implementation types should be ignored on type ctors
1058 typeCtor.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
1060 // private immplementation type should be ignored on type ctors
1061 typeCtor.PrivateImplementationType = new CodeTypeReference (typeof (int));
1063 // return type should be ignored on type ctors
1064 typeCtor.ReturnType = new CodeTypeReference (typeof (int));
1066 // return TypeDeclaration custom attributes
1067 attrDec = new CodeAttributeDeclaration ();
1069 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
1070 new CodePrimitiveExpression (false)));
1071 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
1072 new CodePrimitiveExpression (true)));
1073 typeCtor.ReturnTypeCustomAttributes.Add (attrDec);
1075 return GenerateCodeFromType (TypeDeclaration, options);
1078 protected string GenerateEntryPointMethod (CodeGeneratorOptions options)
1080 TypeDeclaration.Name = "Test1";
1082 CodeEntryPointMethod method = new CodeEntryPointMethod ();
1083 method.Name = "Something"; // should be ignored in C# and VB
1084 method.Attributes = MemberAttributes.Private ; // should be ignored in C# and VB
1085 method.ReturnType = new CodeTypeReference (typeof (int)); // should be ignored in C# 1.x and VB 1.x and 2.x
1087 // parameters on entry point are ignored in C# and VB
1088 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
1089 typeof (object), "value1");
1090 method.Parameters.Add (param);
1092 // custom attributes on entry point are ignored in C# 1.x and VB 1.x
1093 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
1095 method.CustomAttributes.Add (attrDec);
1097 CodeVariableDeclarationStatement v = new CodeVariableDeclarationStatement ("Test+InnerType", "x");
1098 method.Statements.Add (v);
1100 TypeDeclaration.Members.Add (method);
1103 CodeTypeDeclaration nestedType = new CodeTypeDeclaration ("InnerType");
1104 TypeDeclaration.Members.Add (nestedType);
1107 return GenerateCodeFromType (TypeDeclaration, options);
1110 protected string GenerateGenericCodeTypeReferences (CodeGeneratorOptions options)
1112 CodeTypeDeclaration td = new CodeTypeDeclaration ("Test");
1113 CodeMemberField f = new CodeMemberField (
1114 new CodeTypeReference ("System.Nullable",
1115 new CodeTypeReference (typeof (int))),
1118 CodeMemberField f2 = new CodeMemberField (
1119 new CodeTypeReference (typeof (int?).GetGenericTypeDefinition ()),
1121 td.Members.Add (f2);
1122 return GenerateCodeFromType (td, options);
1125 protected string GeneratePartialType (CodeGeneratorOptions options)
1127 TypeDeclaration.Name = "Test1";
1128 TypeDeclaration.IsPartial = true;
1129 return GenerateCodeFromType (TypeDeclaration, options);