Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 How to Generate Code Using Custom A...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
How to Generate Code Using Custom Activities

You can generate code for a custom activity by using a type derived from ActivityCodeGenerator or CompositeActivityCodeGenerator and associating it with your custom activity by using the ActivityCodeGeneratorAttribute. You must override GenerateCode, which will be called during workflow compilation and allow you to add additional code to the workflow partial class that is generated by the compiler, in the form of CodeDOM, before it is passed on to the C# compiler. This scenario can be useful for Web service activities, where you can generate a proxy class on the fly, or other scenarios where the activity needs to emit code based on its configuration for use at run time.

The following example shows how to generate code for your custom activity.

C#
public override void GenerateCode(CodeGenerationManager manager, object obj)
{
    if (manager == null)
    {
        throw new ArgumentNullException("The CodeGenerationManager object is null.");
    }

    if (obj == null)
    {
        throw new ArgumentNullException("The object to generate code for is null.");
    }

    // Cast obj to your custom activity for access to its members.
    Activity1 customActivity = obj as Activity1;

    if (customActivity == null)
    {
        throw new ArgumentException("The obj variable cannot be cast to the Activity type.");
    }

    // Retrieve a type provider object.
    ITypeProvider typeProvider = (ITypeProvider)manager.GetService(typeof(ITypeProvider));

    if (typeProvider == null)
    {
        throw new InvalidOperationException();
    }

    // TODO: Use types in the System.CodeDOM namespace to generate 
    // code. 
}

To associate your custom code generator with your custom activity, you must decorate your activity with the ActivityCodeGeneratorAttribute as shown in the following example.

C#
[ActivityCodeGenerator(typeof(CustomCodeGenerator))]

For more information about using CodeDOM, see Using the CodeDOM.

ms734652.note(en-us,VS.90).gifNote:
You do not have to use CodeDomProvider or any of the CodeDOM compiler types in the System.CodeDOM.Compiler namespace when using CodeDOM in your custom ActivityCodeGenerator implementation.

See Also



Copyright © 2007 by Microsoft Corporation. All rights reserved.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker