Skip to content

Commit

Permalink
Merged attributes to parameters
Browse files Browse the repository at this point in the history
--HG--
branch : parameters
  • Loading branch information
ssimek committed Oct 16, 2009
2 parents 3996770 + 8e68c3e commit 41ee6ac
Show file tree
Hide file tree
Showing 19 changed files with 777 additions and 10 deletions.
71 changes: 71 additions & 0 deletions Examples/A2_Attributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2009, Stefan Simek
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace TriAxis.RunSharp.Examples
{
static class A2_Attributes
{
public static void GenTypeAttributeTest(AssemblyGen ag)
{
TypeGen MyAttribute = ag.Public.Class("MyAttribute", typeof(Attribute))
.BeginAttribute(typeof(AttributeUsageAttribute), AttributeTargets.Class).Set("AllowMultiple", true).End()
;
FieldGen testField = MyAttribute.Field(typeof(object), "testField")
.Attribute(typeof(DescriptionAttribute), "Test field")
;
PropertyGen testProperty = MyAttribute.Public.SimpleProperty(testField, "TestProperty")
.Attribute(typeof(ObsoleteAttribute), "Do not use this")
;

testProperty.Getter().Attribute(typeof(DescriptionAttribute), "Getter method");
testProperty.Getter().ReturnParameter.Attribute(typeof(DescriptionAttribute), "Getter return value");
testProperty.Setter().Attribute(typeof(DescriptionAttribute), "Setter method");

TypeGen tg = ag.Class("Test")
.BeginAttribute(MyAttribute).Set("TestProperty", 3).End()
.Attribute(typeof(System.ComponentModel.DescriptionAttribute), "Test class")
;

tg.Static.Method(typeof(void), "Main")
.BeginAttribute(MyAttribute).Set("TestProperty", 3).End()
;

TypeGen SimpleDelegate = ag.Delegate(typeof(void), "SimpleDelegate")
.Attribute(typeof(DescriptionAttribute), "Test delegate")
;

EventGen TestEvent = tg.Static.Event(SimpleDelegate, "TestEvent")
.Attribute(typeof(DescriptionAttribute), "Test event")
;

TestEvent.AddMethod().Attribute(typeof(DescriptionAttribute), "Event add method");
TestEvent.RemoveMethod().Attribute(typeof(DescriptionAttribute), "Event remove method");
}
}
}
9 changes: 6 additions & 3 deletions Examples/Examples.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="2.0">
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="2.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{2267D578-07C3-46B8-9EC8-E2C33AC920E7}</ProjectGuid>
<OutputType>Exe</OutputType>
Expand Down Expand Up @@ -48,6 +48,9 @@
<Compile Include="13_Events.cs" />
<Compile Include="14_ExplicitImplementation.cs" />
<Compile Include="A1_BreakContinue.cs" />
<Compile Include="A2_Attributes.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Bugs\X1864084_ValueTypeVirtual.cs" />
Expand All @@ -66,4 +69,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
53 changes: 53 additions & 0 deletions RunSharp/AssemblyGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class AssemblyGen
AssemblyBuilder asm;
ModuleBuilder mod;
List<TypeGen> types = new List<TypeGen>();
List<AttributeGen> assemblyAttributes;
List<AttributeGen> moduleAttributes;
string ns = null;

internal AssemblyBuilder AssemblyBuilder { get { return asm; } }
Expand Down Expand Up @@ -94,6 +96,54 @@ string Qualify(string name)
public AssemblyGen NoBeforeFieldInit { get { attrs |= TypeAttributes.BeforeFieldInit; return this; } }
#endregion

#region Custom Attributes

public AssemblyGen Attribute(AttributeType type)
{
BeginAttribute(type);
return this;
}

public AssemblyGen Attribute(AttributeType type, params object[] args)
{
BeginAttribute(type, args);
return this;
}

public AttributeGen<AssemblyGen> BeginAttribute(AttributeType type)
{
return BeginAttribute(type, EmptyArray<object>.Instance);
}

public AttributeGen<AssemblyGen> BeginAttribute(AttributeType type, params object[] args)
{
return AttributeGen<AssemblyGen>.CreateAndAdd(this, ref assemblyAttributes, AttributeTargets.Assembly, type, args);
}

public AssemblyGen ModuleAttribute(AttributeType type)
{
BeginModuleAttribute(type);
return this;
}

public AssemblyGen ModuleAttribute(AttributeType type, params object[] args)
{
BeginModuleAttribute(type, args);
return this;
}

public AttributeGen<AssemblyGen> BeginModuleAttribute(AttributeType type)
{
return BeginModuleAttribute(type, EmptyArray<object>.Instance);
}

public AttributeGen<AssemblyGen> BeginModuleAttribute(AttributeType type, params object[] args)
{
return AttributeGen<AssemblyGen>.CreateAndAdd(this, ref moduleAttributes, AttributeTargets.Module, type, args);
}

#endregion

#region Types
public TypeGen Class(string name)
{
Expand Down Expand Up @@ -205,6 +255,9 @@ public void Complete()
{
foreach (TypeGen tg in types)
tg.Complete();

AttributeGen.ApplyList(ref assemblyAttributes, asm.SetCustomAttribute);
AttributeGen.ApplyList(ref moduleAttributes, mod.SetCustomAttribute);
}
#endregion
}
Expand Down
Loading

0 comments on commit 41ee6ac

Please sign in to comment.