2 // Base class for CodeGenerator.GenerateCodeFromType unit tests.
5 // Gert Driesen (drieseng@users.sourceforge.net)
11 using System.CodeDom.Compiler;
12 using System.Reflection;
13 using System.Security;
14 using System.Security.Principal;
16 using NUnit.Framework;
18 namespace MonoTests.System.CodeDom.Compiler
20 public abstract class CodeGeneratorFromTypeTestBase : CodeGeneratorTestBase
22 protected abstract CodeTypeDeclaration TypeDeclaration
28 public abstract void DefaultTypeTest ();
31 public abstract void NullTypeTest ();
34 public abstract void SimpleTypeTest ();
37 public abstract void DerivedTypeTest ();
40 public abstract void AttributesAndTypeTest ();
43 public abstract void EventMembersTypeTest1 ();
46 public abstract void EventMembersTypeTest2 ();
49 public abstract void EventImplementationTypes ();
52 public abstract void EventPrivateImplementationType ();
55 public abstract void EventImplementationTypeOrder ();
58 public abstract void FieldMembersAttributesTest ();
61 public abstract void FieldMembersTypeTest ();
64 public abstract void FieldNewSlotTest ();
67 public abstract void PropertyMembersTypeTest1 ();
70 public abstract void PropertyMembersTypeTest2 ();
73 public abstract void PropertyMembersTypeGetOnly ();
76 public abstract void PropertyMembersTypeSetOnly ();
79 public abstract void PropertyMembersTypeGetSet ();
82 public abstract void PropertyMembersTypeFamilyOrAssembly ();
85 public abstract void PropertyMembersTypeAssembly ();
88 public abstract void PropertyParametersTest ();
91 public abstract void PropertyIndexerTest1 ();
94 public abstract void PropertyIndexerTest2 ();
97 public abstract void PropertyIndexerGetOnly ();
100 public abstract void PropertyIndexerSetOnly ();
103 public abstract void PropertyImplementationTypes ();
106 public abstract void PropertyOverloadsTest1 ();
109 public abstract void PropertyOverloadsTest2 ();
112 public abstract void PropertyOverloadsTest3 ();
115 public abstract void PropertyPrivateImplementationType ();
118 public abstract void PropertyImplementationTypeOrder ();
121 public abstract void PropertyNewSlotTest ();
124 public abstract void MethodMembersTypeTest1 ();
127 public abstract void MethodMembersTypeTest2 ();
130 public abstract void MethodMembersTypeTest3 ();
133 public abstract void MethodImplementationTypes ();
136 public abstract void MethodOverloadsTest1 ();
139 public abstract void MethodOverloadsTest2 ();
142 public abstract void MethodOverloadsTest3 ();
145 public abstract void MethodPrivateImplementationType ();
148 public abstract void MethodImplementationTypeOrder ();
151 public abstract void MethodReturnTypeAttributes ();
154 public abstract void MethodNewSlotTest ();
157 public abstract void ConstructorAttributesTest ();
160 public abstract void ConstructorParametersTest ();
163 public abstract void ConstructorParameterAttributesTest ();
166 public abstract void BaseConstructorSingleArg ();
169 public abstract void BaseConstructorMultipleArgs ();
172 public abstract void ChainedConstructorSingleArg ();
175 public abstract void ChainedConstructorMultipleArgs ();
178 public abstract void TypeConstructorTest ();
181 public abstract void EntryPointMethodTest ();
183 protected string GenerateDefaultType (CodeGeneratorOptions options)
185 return GenerateCodeFromType (TypeDeclaration, options);
188 protected string GenerateNullType (CodeGeneratorOptions options)
190 return GenerateCodeFromType (null, options);
193 protected string GenerateSimpleType (CodeGeneratorOptions options)
195 TypeDeclaration.Name = "Test1";
196 return GenerateCodeFromType (TypeDeclaration, options);
199 protected string GenerateDerivedType (CodeGeneratorOptions options)
201 TypeDeclaration.Name = "Test1";
202 TypeDeclaration.TypeAttributes |= TypeAttributes.NestedFamily |
203 TypeAttributes.Abstract;
204 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (int)));
205 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (IIdentity)));
206 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (string)));
207 TypeDeclaration.BaseTypes.Add (new CodeTypeReference (typeof (IPermission)));
208 return GenerateCodeFromType (TypeDeclaration, options);
211 protected string GenerateAttributesAndType (CodeGeneratorOptions options)
213 TypeDeclaration.Name = "Test1";
215 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
217 TypeDeclaration.CustomAttributes.Add (attrDec);
219 attrDec = new CodeAttributeDeclaration ();
221 TypeDeclaration.CustomAttributes.Add (attrDec);
223 return GenerateCodeFromType (TypeDeclaration, options);
226 protected string GenerateEventMembersType1 (CodeGeneratorOptions options)
228 TypeDeclaration.Name = "Test1";
230 CodeMemberEvent evt = new CodeMemberEvent ();
232 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
234 evt.CustomAttributes.Add (attrDec);
236 attrDec = new CodeAttributeDeclaration ();
238 evt.CustomAttributes.Add (attrDec);
240 TypeDeclaration.Members.Add (evt);
242 return GenerateCodeFromType (TypeDeclaration, options);
245 protected string GenerateEventMembersType2 (CodeGeneratorOptions options)
247 TypeDeclaration.Name = "Test1";
249 CodeMemberEvent evt = new CodeMemberEvent ();
251 evt.Attributes = MemberAttributes.Public | MemberAttributes.Override
252 | MemberAttributes.Static | MemberAttributes.Abstract |
253 MemberAttributes.New;
254 evt.Type = new CodeTypeReference (typeof (int));
255 TypeDeclaration.Members.Add (evt);
257 return GenerateCodeFromType (TypeDeclaration, options);
260 protected string GenerateEventImplementationTypes (CodeGeneratorOptions options)
262 TypeDeclaration.Name = "Test1";
264 CodeMemberEvent evt = new CodeMemberEvent ();
266 evt.Attributes = MemberAttributes.FamilyAndAssembly;
267 evt.Type = new CodeTypeReference (typeof (int));
268 evt.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
269 evt.ImplementationTypes.Add (new CodeTypeReference ("IWhatever"));
270 TypeDeclaration.Members.Add (evt);
272 return GenerateCodeFromType (TypeDeclaration, options);
275 protected string GenerateEventPrivateImplementationType (CodeGeneratorOptions options)
277 TypeDeclaration.Name = "Test1";
279 CodeMemberEvent evt = new CodeMemberEvent ();
281 evt.Attributes = MemberAttributes.Family | MemberAttributes.Overloaded;
282 evt.Type = new CodeTypeReference (typeof (int));
283 evt.PrivateImplementationType = new CodeTypeReference (typeof (int));
284 TypeDeclaration.Members.Add (evt);
286 return GenerateCodeFromType (TypeDeclaration, options);
289 protected string GenerateEventImplementationTypeOrder (CodeGeneratorOptions options)
291 TypeDeclaration.Name = "Test1";
293 CodeMemberEvent evt = new CodeMemberEvent ();
295 evt.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
296 evt.Type = new CodeTypeReference (typeof (int));
297 evt.PrivateImplementationType = new CodeTypeReference (typeof (int));
298 evt.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
299 TypeDeclaration.Members.Add (evt);
301 return GenerateCodeFromType (TypeDeclaration, options);
304 protected string GenerateFieldMembersAttributes (CodeGeneratorOptions options)
306 TypeDeclaration.Name = "Test1";
308 CodeMemberField fld = new CodeMemberField ();
310 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
312 fld.CustomAttributes.Add (attrDec);
314 attrDec = new CodeAttributeDeclaration ();
316 fld.CustomAttributes.Add (attrDec);
318 TypeDeclaration.Members.Add (fld);
320 return GenerateCodeFromType (TypeDeclaration, options);
323 protected string GenerateFieldMembersType (MemberAttributes memberAttributes, CodeGeneratorOptions options)
325 TypeDeclaration.Name = "Test1";
327 CodeMemberField fld = new CodeMemberField ();
329 fld.Attributes = memberAttributes;
330 fld.Type = new CodeTypeReference (typeof (int));
331 fld.InitExpression = new CodePrimitiveExpression (2);
332 TypeDeclaration.Members.Add (fld);
334 return GenerateCodeFromType (TypeDeclaration, options);
337 protected string GeneratePropertyMembersAttributes (CodeGeneratorOptions options)
339 TypeDeclaration.Name = "Test1";
341 CodeMemberProperty property = new CodeMemberProperty ();
342 TypeDeclaration.Members.Add (property);
344 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
346 property.CustomAttributes.Add (attrDec);
348 attrDec = new CodeAttributeDeclaration ();
350 property.CustomAttributes.Add (attrDec);
352 return GenerateCodeFromType (TypeDeclaration, options);
355 protected string GeneratePropertyMembersType (MemberAttributes memberAttributes, bool hasGet, bool hasSet, CodeGeneratorOptions options)
357 TypeDeclaration.Name = "Test1";
359 CodeMemberProperty property = new CodeMemberProperty ();
360 property.Name = "Name";
361 property.Attributes = memberAttributes;
362 property.HasGet = hasGet;
363 property.HasSet = hasSet;
364 property.Type = new CodeTypeReference (typeof (int));
365 TypeDeclaration.Members.Add (property);
367 return GenerateCodeFromType (TypeDeclaration, options);
370 protected string GeneratePropertyParameters (CodeGeneratorOptions options)
372 TypeDeclaration.Name = "Test1";
374 CodeMemberProperty property = new CodeMemberProperty ();
375 property.Name = "Name";
376 property.Attributes = MemberAttributes.Public;
377 property.Type = new CodeTypeReference (typeof (int));
379 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
380 typeof (object), "value1");
381 property.Parameters.Add (param);
383 param = new CodeParameterDeclarationExpression (
384 typeof (int), "value2");
385 param.Direction = FieldDirection.Ref;
386 property.Parameters.Add (param);
388 TypeDeclaration.Members.Add (property);
390 return GenerateCodeFromType (TypeDeclaration, options);
393 protected string GeneratePropertyIndexer (MemberAttributes memberAttributes, bool hasGet, bool hasSet, bool addParameters, CodeGeneratorOptions options)
395 TypeDeclaration.Name = "Test1";
397 CodeMemberProperty property = new CodeMemberProperty ();
398 // ensure case-insensitive comparison is done on name of property
399 property.Name = "iTem";
400 property.Attributes = memberAttributes;
401 property.HasGet = hasGet;
402 property.HasSet = hasSet;
403 property.Type = new CodeTypeReference (typeof (int));
404 TypeDeclaration.Members.Add (property);
407 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
408 typeof (object), "value1");
409 property.Parameters.Add (param);
411 param = new CodeParameterDeclarationExpression (
412 typeof (int), "value2");
413 param.Direction = FieldDirection.Ref;
414 property.Parameters.Add (param);
417 return GenerateCodeFromType (TypeDeclaration, options);
420 protected string GeneratePropertyImplementationTypes (CodeGeneratorOptions options)
422 TypeDeclaration.Name = "Test1";
424 CodeMemberProperty property = new CodeMemberProperty ();
425 property.Name = "Name";
426 property.Attributes = MemberAttributes.Public;
427 property.Type = new CodeTypeReference (typeof (int));
428 property.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
429 property.ImplementationTypes.Add (new CodeTypeReference ("IWhatever"));
430 TypeDeclaration.Members.Add (property);
432 return GenerateCodeFromType (TypeDeclaration, options);
435 protected string GeneratePropertyOverloads1 (CodeGeneratorOptions options)
437 TypeDeclaration.Name = "Test1";
439 CodeMemberProperty property = new CodeMemberProperty ();
440 property.Name = "Name";
441 property.Attributes = MemberAttributes.Family | MemberAttributes.Overloaded;
442 property.Type = new CodeTypeReference (typeof (int));
443 TypeDeclaration.Members.Add (property);
445 return GenerateCodeFromType (TypeDeclaration, options);
448 protected string GeneratePropertyOverloads2 (CodeGeneratorOptions options)
450 TypeDeclaration.Name = "Test1";
452 CodeMemberProperty property = new CodeMemberProperty ();
453 property.Name = "Name";
454 property.Attributes = MemberAttributes.Public;
455 property.Type = new CodeTypeReference (typeof (int));
456 TypeDeclaration.Members.Add (property);
458 property = new CodeMemberProperty ();
459 property.Name = "Name";
460 property.Attributes = MemberAttributes.Private;
461 property.Type = new CodeTypeReference (typeof (int));
462 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
463 typeof (object), "value1");
464 property.Parameters.Add (param);
465 TypeDeclaration.Members.Add (property);
467 return GenerateCodeFromType (TypeDeclaration, options);
470 protected string GeneratePropertyOverloads3 (CodeGeneratorOptions options)
472 TypeDeclaration.Name = "Test1";
474 CodeMemberProperty property = new CodeMemberProperty ();
475 property.Name = "Name";
476 property.Attributes = MemberAttributes.Public;
477 property.Type = new CodeTypeReference (typeof (int));
478 TypeDeclaration.Members.Add (property);
480 property = new CodeMemberProperty ();
481 property.Name = "Name";
482 property.Attributes = MemberAttributes.Private;
483 property.Type = new CodeTypeReference (typeof (int));
484 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
485 typeof (object), "value1");
486 property.Parameters.Add (param);
487 property.PrivateImplementationType = new CodeTypeReference (typeof (int));
488 TypeDeclaration.Members.Add (property);
490 return GenerateCodeFromType (TypeDeclaration, options);
493 protected string GeneratePropertyPrivateImplementationType (CodeGeneratorOptions options)
495 TypeDeclaration.Name = "Test1";
497 CodeMemberProperty property = new CodeMemberProperty ();
498 property.Name = "Item";
499 property.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
500 property.Type = new CodeTypeReference (typeof (int));
501 property.PrivateImplementationType = 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 GeneratePropertyImplementationTypeOrder (CodeGeneratorOptions options)
512 TypeDeclaration.Name = "Test1";
514 CodeMemberProperty property = new CodeMemberProperty ();
515 property.Name = "Item";
516 property.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
517 property.Type = new CodeTypeReference (typeof (int));
518 property.PrivateImplementationType = new CodeTypeReference (typeof (int));
519 property.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
520 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
521 typeof (object), "value1");
522 property.Parameters.Add (param);
523 TypeDeclaration.Members.Add (property);
525 return GenerateCodeFromType (TypeDeclaration, options);
528 protected string GenerateMethodMembersType1 (CodeGeneratorOptions options)
530 TypeDeclaration.Name = "Test1";
532 CodeMemberMethod method = new CodeMemberMethod ();
534 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
536 method.CustomAttributes.Add (attrDec);
538 attrDec = new CodeAttributeDeclaration ();
540 method.CustomAttributes.Add (attrDec);
542 TypeDeclaration.Members.Add (method);
544 return GenerateCodeFromType (TypeDeclaration, options);
547 protected string GenerateMethodMembersType2 (CodeGeneratorOptions options)
549 TypeDeclaration.Name = "Test1";
551 CodeMemberMethod method = new CodeMemberMethod ();
552 method.Name = "Something";
553 method.Attributes = MemberAttributes.Public;
554 method.ReturnType = new CodeTypeReference (typeof (int));
556 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
557 typeof (object), "value1");
558 method.Parameters.Add (param);
560 param = new CodeParameterDeclarationExpression (
561 typeof (object), "value2");
562 param.Direction = FieldDirection.In;
563 method.Parameters.Add (param);
565 param = new CodeParameterDeclarationExpression (typeof (int), "index");
566 param.Direction = FieldDirection.Out;
567 method.Parameters.Add (param);
569 param = new CodeParameterDeclarationExpression (typeof (int), "count");
570 param.Direction = FieldDirection.Ref;
571 method.Parameters.Add (param);
573 TypeDeclaration.Members.Add (method);
575 return GenerateCodeFromType (TypeDeclaration, options);
578 protected string GenerateMethodMembersType3 (CodeGeneratorOptions options)
580 TypeDeclaration.Name = "Test1";
582 CodeMemberMethod method = new CodeMemberMethod ();
583 method.Name = "Something";
584 method.Attributes = MemberAttributes.Public;
585 method.ReturnType = new CodeTypeReference (typeof (int));
588 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
589 typeof (object), "value");
590 method.Parameters.Add (param);
592 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
594 param.CustomAttributes.Add (attrDec);
596 attrDec = new CodeAttributeDeclaration ();
598 param.CustomAttributes.Add (attrDec);
601 param = new CodeParameterDeclarationExpression (typeof (int), null);
602 param.Direction = FieldDirection.Out;
603 method.Parameters.Add (param);
605 attrDec = new CodeAttributeDeclaration ();
607 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
608 new CodePrimitiveExpression (false)));
609 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
610 new CodePrimitiveExpression (true)));
611 param.CustomAttributes.Add (attrDec);
613 attrDec = new CodeAttributeDeclaration ();
615 param.CustomAttributes.Add (attrDec);
617 TypeDeclaration.Members.Add (method);
619 return GenerateCodeFromType (TypeDeclaration, options);
622 protected string GenerateMethodImplementationTypes (CodeGeneratorOptions options)
624 TypeDeclaration.Name = "Test1";
626 CodeMemberMethod method = new CodeMemberMethod ();
627 method.Name = "Execute";
628 method.Attributes = MemberAttributes.Assembly;
629 method.ReturnType = new CodeTypeReference (typeof (int));
630 method.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
631 method.ImplementationTypes.Add (new CodeTypeReference ("IWhatever"));
632 TypeDeclaration.Members.Add (method);
634 return GenerateCodeFromType (TypeDeclaration, options);
637 protected string GenerateMethodOverloads1 (CodeGeneratorOptions options)
639 TypeDeclaration.Name = "Test1";
641 CodeMemberMethod method = new CodeMemberMethod ();
642 method.Name = "Execute";
643 method.Attributes = MemberAttributes.Assembly | MemberAttributes.Overloaded;
644 method.ReturnType = new CodeTypeReference (typeof (int));
645 TypeDeclaration.Members.Add (method);
647 return GenerateCodeFromType (TypeDeclaration, options);
650 protected string GenerateMethodOverloads2 (CodeGeneratorOptions options)
652 TypeDeclaration.Name = "Test1";
654 CodeMemberMethod method = new CodeMemberMethod ();
655 method.Name = "Execute";
656 method.Attributes = MemberAttributes.Public;
657 TypeDeclaration.Members.Add (method);
659 method = new CodeMemberMethod ();
660 method.Name = "Execute";
661 method.Attributes = MemberAttributes.Private;
662 method.ReturnType = new CodeTypeReference (typeof (int));
663 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
664 typeof (object), "value1");
665 method.Parameters.Add (param);
666 TypeDeclaration.Members.Add (method);
668 return GenerateCodeFromType (TypeDeclaration, options);
671 protected string GenerateMethodOverloads3 (CodeGeneratorOptions options)
673 TypeDeclaration.Name = "Test1";
675 CodeMemberMethod method = new CodeMemberMethod ();
676 method.Name = "Execute";
677 method.Attributes = MemberAttributes.Public;
678 TypeDeclaration.Members.Add (method);
680 method = new CodeMemberMethod ();
681 method.Name = "Execute";
682 method.Attributes = MemberAttributes.Private;
683 method.ReturnType = new CodeTypeReference (typeof (int));
684 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
685 typeof (object), "value1");
686 method.Parameters.Add (param);
687 method.PrivateImplementationType = new CodeTypeReference (typeof (int));
688 TypeDeclaration.Members.Add (method);
690 return GenerateCodeFromType (TypeDeclaration, options);
693 protected string GenerateMethodPrivateImplementationType (CodeGeneratorOptions options)
695 TypeDeclaration.Name = "Test1";
697 CodeMemberMethod method = new CodeMemberMethod ();
698 method.Name = "Execute";
699 method.Attributes = MemberAttributes.Public | MemberAttributes.Overloaded;
700 method.ReturnType = new CodeTypeReference (typeof (int));
701 method.PrivateImplementationType = new CodeTypeReference (typeof (int));
702 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
703 typeof (object), "value1");
704 method.Parameters.Add (param);
705 TypeDeclaration.Members.Add (method);
707 return GenerateCodeFromType (TypeDeclaration, options);
710 protected string GenerateMethodImplementationTypeOrder (CodeGeneratorOptions options)
712 TypeDeclaration.Name = "Test1";
714 CodeMemberMethod method = new CodeMemberMethod ();
715 method.Name = "Execute";
716 method.Attributes = MemberAttributes.Public;
717 method.ReturnType = new CodeTypeReference (typeof (int));
718 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
719 typeof (object), "value1");
720 method.Parameters.Add (param);
721 method.PrivateImplementationType = new CodeTypeReference (typeof (int));
722 method.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
723 TypeDeclaration.Members.Add (method);
725 return GenerateCodeFromType (TypeDeclaration, options);
728 protected string GenerateMethodReturnTypeAttributes (CodeGeneratorOptions options)
730 TypeDeclaration.Name = "Test1";
732 CodeMemberMethod method = new CodeMemberMethod ();
733 method.Name = "Execute";
734 method.Attributes = MemberAttributes.Public;
735 method.ReturnType = new CodeTypeReference (typeof (int));
736 TypeDeclaration.Members.Add (method);
738 // method custom attributes
739 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
741 method.CustomAttributes.Add (attrDec);
743 attrDec = new CodeAttributeDeclaration ();
745 method.CustomAttributes.Add (attrDec);
747 // return TypeDeclaration custom attributes
748 attrDec = new CodeAttributeDeclaration ();
750 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
751 new CodePrimitiveExpression (false)));
752 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
753 new CodePrimitiveExpression (true)));
754 method.ReturnTypeCustomAttributes.Add (attrDec);
756 attrDec = new CodeAttributeDeclaration ();
758 method.ReturnTypeCustomAttributes.Add (attrDec);
760 return GenerateCodeFromType (TypeDeclaration, options);
763 protected string GenerateConstructorAttributes (CodeGeneratorOptions options)
765 TypeDeclaration.Name = "Test1";
767 CodeConstructor ctor = new CodeConstructor ();
769 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
771 ctor.CustomAttributes.Add (attrDec);
773 attrDec = new CodeAttributeDeclaration ();
775 ctor.CustomAttributes.Add (attrDec);
777 TypeDeclaration.Members.Add (ctor);
779 return GenerateCodeFromType (TypeDeclaration, options);
782 protected string GenerateMethodNewSlot (CodeGeneratorOptions options)
784 TypeDeclaration.Name = "Test1";
786 CodeMemberMethod method = new CodeMemberMethod ();
787 method.Name = "Execute";
788 method.Attributes = MemberAttributes.Public | MemberAttributes.New;
789 method.ReturnType = new CodeTypeReference (typeof (int));
790 TypeDeclaration.Members.Add (method);
792 return GenerateCodeFromType (TypeDeclaration, options);
795 protected string GenerateConstructorParameters (CodeGeneratorOptions options)
797 TypeDeclaration.Name = "Test1";
799 CodeConstructor ctor = new CodeConstructor ();
800 ctor.Name = "Whatever";
801 ctor.Attributes = MemberAttributes.Public;
803 // scope and vtable modifiers should be ignored
804 ctor.Attributes |= MemberAttributes.Abstract | MemberAttributes.Const
805 | MemberAttributes.Final | MemberAttributes.New
806 | MemberAttributes.Overloaded | MemberAttributes.Override
807 | MemberAttributes.Static;
809 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
810 typeof (object), "value1");
811 ctor.Parameters.Add (param);
813 param = new CodeParameterDeclarationExpression (
814 typeof (object), "value2");
815 param.Direction = FieldDirection.In;
816 ctor.Parameters.Add (param);
818 param = new CodeParameterDeclarationExpression (typeof (int), "index");
819 param.Direction = FieldDirection.Out;
820 ctor.Parameters.Add (param);
822 param = new CodeParameterDeclarationExpression (typeof (int), "count");
823 param.Direction = FieldDirection.Ref;
824 ctor.Parameters.Add (param);
826 TypeDeclaration.Members.Add (ctor);
828 return GenerateCodeFromType (TypeDeclaration, options);
831 protected string GenerateConstructorParameterAttributes (CodeGeneratorOptions options)
833 TypeDeclaration.Name = "Test1";
835 CodeConstructor ctor = new CodeConstructor ();
836 ctor.Name = "Something";
837 ctor.Attributes = MemberAttributes.Private;
840 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
841 typeof (object), "value");
842 ctor.Parameters.Add (param);
844 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
846 param.CustomAttributes.Add (attrDec);
848 attrDec = new CodeAttributeDeclaration ();
850 param.CustomAttributes.Add (attrDec);
853 param = new CodeParameterDeclarationExpression (typeof (int), "index");
854 param.Direction = FieldDirection.Out;
855 ctor.Parameters.Add (param);
857 attrDec = new CodeAttributeDeclaration ();
859 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
860 new CodePrimitiveExpression (false)));
861 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
862 new CodePrimitiveExpression (true)));
863 param.CustomAttributes.Add (attrDec);
865 attrDec = new CodeAttributeDeclaration ();
867 param.CustomAttributes.Add (attrDec);
869 TypeDeclaration.Members.Add (ctor);
871 return GenerateCodeFromType (TypeDeclaration, options);
874 protected string GenerateBaseConstructor (bool multipleArgs, CodeGeneratorOptions options)
876 TypeDeclaration.Name = "Test1";
878 CodeConstructor ctor = new CodeConstructor ();
879 ctor.Name = "Something";
880 ctor.Attributes = MemberAttributes.Family;
883 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
884 typeof (object), "value1");
885 ctor.Parameters.Add (param);
888 param = new CodeParameterDeclarationExpression (typeof (int), "value2");
889 param.Direction = FieldDirection.Out;
890 ctor.Parameters.Add (param);
893 ctor.BaseConstructorArgs.Add (new CodeVariableReferenceExpression ("value1"));
896 ctor.BaseConstructorArgs.Add (new CodeVariableReferenceExpression ("value2"));
899 TypeDeclaration.Members.Add (ctor);
901 return GenerateCodeFromType (TypeDeclaration, options);
904 protected string GenerateChainedConstructor (bool multipleArgs, CodeGeneratorOptions options)
906 TypeDeclaration.Name = "Test1";
908 CodeConstructor ctor = new CodeConstructor ();
909 ctor.Name = "Something";
910 ctor.Attributes = MemberAttributes.Public;
913 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
914 typeof (object), "value1");
915 ctor.Parameters.Add (param);
918 param = new CodeParameterDeclarationExpression (typeof (int), "value2");
919 param.Direction = FieldDirection.Out;
920 ctor.Parameters.Add (param);
922 // implementation types should be ignored on ctors
923 ctor.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
926 ctor.ChainedConstructorArgs.Add (new CodeVariableReferenceExpression ("value1"));
929 ctor.ChainedConstructorArgs.Add (new CodeVariableReferenceExpression ("value2"));
932 // should be ignored as chained ctor args should take precedence over base
934 ctor.BaseConstructorArgs.Add (new CodeVariableReferenceExpression ("value3"));
936 TypeDeclaration.Members.Add (ctor);
938 return GenerateCodeFromType (TypeDeclaration, options);
941 protected string GenerateTypeConstructor (CodeGeneratorOptions options)
943 TypeDeclaration.Name = "Test1";
945 CodeTypeConstructor typeCtor = new CodeTypeConstructor ();
946 // access, scope and vtable modifiers should be ignored
947 typeCtor.Attributes |= MemberAttributes.Public | MemberAttributes.Abstract
948 | MemberAttributes.Const | MemberAttributes.Final
949 | MemberAttributes.New | MemberAttributes.Overloaded
950 | MemberAttributes.Override | MemberAttributes.Static;
951 TypeDeclaration.Members.Add (typeCtor);
954 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
956 typeCtor.CustomAttributes.Add (attrDec);
958 attrDec = new CodeAttributeDeclaration ();
960 typeCtor.CustomAttributes.Add (attrDec);
962 // parameter should be ignored
963 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
964 typeof (object), "value1");
965 typeCtor.Parameters.Add (param);
967 // implementation types should be ignored on type ctors
968 typeCtor.ImplementationTypes.Add (new CodeTypeReference ("IPolicy"));
970 // private immplementation type should be ignored on type ctors
971 typeCtor.PrivateImplementationType = new CodeTypeReference (typeof (int));
973 // return type should be ignored on type ctors
974 typeCtor.ReturnType = new CodeTypeReference (typeof (int));
976 // return TypeDeclaration custom attributes
977 attrDec = new CodeAttributeDeclaration ();
979 attrDec.Arguments.Add (new CodeAttributeArgument ("A1",
980 new CodePrimitiveExpression (false)));
981 attrDec.Arguments.Add (new CodeAttributeArgument ("A2",
982 new CodePrimitiveExpression (true)));
983 typeCtor.ReturnTypeCustomAttributes.Add (attrDec);
985 return GenerateCodeFromType (TypeDeclaration, options);
988 protected string GenerateEntryPointMethod (CodeGeneratorOptions options)
990 TypeDeclaration.Name = "Test1";
992 CodeEntryPointMethod method = new CodeEntryPointMethod ();
993 method.Name = "Something"; // should be ignored in C# and VB
994 method.Attributes = MemberAttributes.Private ; // should be ignored in C# and VB
995 method.ReturnType = new CodeTypeReference (typeof (int)); // should be ignored in C# 1.x and VB 1.x and 2.x
997 // parameters on entry point are ignored in C# and VB
998 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (
999 typeof (object), "value1");
1000 method.Parameters.Add (param);
1002 // custom attributes on entry point are ignored in C# 1.x and VB 1.x
1003 CodeAttributeDeclaration attrDec = new CodeAttributeDeclaration ();
1005 method.CustomAttributes.Add (attrDec);
1007 CodeVariableDeclarationStatement v = new CodeVariableDeclarationStatement ("Test+InnerType", "x");
1008 method.Statements.Add (v);
1010 TypeDeclaration.Members.Add (method);
1013 CodeTypeDeclaration nestedType = new CodeTypeDeclaration ("InnerType");
1014 TypeDeclaration.Members.Add (nestedType);
1017 return GenerateCodeFromType (TypeDeclaration, options);