locked
How can I get the activity .NET type from loose XAML? RRS feed

  • Question

  • I am with loading a workflow from .xaml using

    ActivityXamlServices.Load
    

    I would like to retrieve the .NET type of my workflow (ie the type x:class references).

    Instead I am only able to retrieve a DynamicActivity or the root activity.

    I see that DynamicActivityXamlReader.xClassName is inaccessailbe due to its protection level (It would be easier to use LINQtoXML than reflection).

    How can I get the activity .NET type from loose XAML?


    Sunday, June 12, 2011 1:06 PM

Answers

  •  Solution:

    var xdoc = XDocument.Load(workflowFileName);
                var attribs = xdoc.Root.Attributes();
                var classAttrib = attribs.Where(a=>a.Name.LocalName=="Class").FirstOrDefault();
                var namespaceAttrib = attribs.Where(a => a.Name.LocalName == "local").FirstOrDefault();
                if (classAttrib == null || namespaceAttrib == nullreturn null;
                var ns = namespaceAttrib.Value.Split('=').Last();
                var assembly = Assembly.Load(new AssemblyName(ns));
                var type = assembly.GetType(classAttrib.Value);

    everytime I need to step outside System.Activities and manipulate XML, use reflection or VisualTreeHelper, it says to me that WF 4 is immature.

     

    • Marked as answer by JReuben1 Sunday, June 12, 2011 2:21 PM
    Sunday, June 12, 2011 2:21 PM

All replies

  •  Solution:

    var xdoc = XDocument.Load(workflowFileName);
                var attribs = xdoc.Root.Attributes();
                var classAttrib = attribs.Where(a=>a.Name.LocalName=="Class").FirstOrDefault();
                var namespaceAttrib = attribs.Where(a => a.Name.LocalName == "local").FirstOrDefault();
                if (classAttrib == null || namespaceAttrib == nullreturn null;
                var ns = namespaceAttrib.Value.Split('=').Last();
                var assembly = Assembly.Load(new AssemblyName(ns));
                var type = assembly.GetType(classAttrib.Value);

    everytime I need to step outside System.Activities and manipulate XML, use reflection or VisualTreeHelper, it says to me that WF 4 is immature.

     

    • Marked as answer by JReuben1 Sunday, June 12, 2011 2:21 PM
    Sunday, June 12, 2011 2:21 PM
  • You could also have used the XamlService from ActivityXamlServices.CreateBuilderReader() to load the XAML as an ActivityBuilder, and read the class name off of that.
    Of course, a namespace + a class name doesn't necessarily tell you the correct assembly to load.

    By the way, what are you commonly using XML, reflection and VisualTreeHelper for, aside from this?
    We really do appreciate submit constructive feedback on how WF4 can be better, whether by this forum, connect.microsoft.com, or any other means.
    Tim

    Monday, June 13, 2011 3:41 PM