次の方法で共有


MSBuild Task Generator: Part 3. Implementing ITask

Yesterday we introduced the ITask interface.  Today we’ll look at a simple implementation of the ITask interface.

 

I’m not a big fan of reading to be people what they can read for themselves so let’s move on to the code…

 

[Caveat: this is sample code. This whole series on the blog is sample code. While I am making a reasonable effort with respect to error handling and design, I am not going to take up a lot of room on the blog with comments and error checking that distracts from the intent of the code.]

 

namespace MBFTaskGenerator

{

      public class MBFTaskFile : ITask

      {

            string m_Namespace;

            string m_Class;

            string m_BaseClass;

            List<ITaskProperty> m_Properties = new List<ITaskProperty>();

            public MBFTaskFile(TasksTask task)

            {

                  Namespace = task.Namespace;

                  Class = task.Class;

                  BaseClass = (task.BaseClass == null) ?

                        typeof(Microsoft.Build.Utilities.Task).FullName : task.BaseClass;

            }

            public IList<ITaskProperty> Properties

            {

                  get

                  {

                        return m_Properties;

                  }

            }

            public string Namespace

            {

                  get

                  {

                        return m_Namespace;

                  }

                  set

                  {

                        m_Namespace = value;

                  }

            }

            public string Class

            {

                  get

                  {

                        return m_Class;

                  }

                  set

                  {

                        m_Class = value;

                  }

            }

            public string BaseClass

            {

                  get

                  {

                  return m_BaseClass;

                  }

                  set

                  {

                        m_BaseClass = value;

                  }

            }

      }

}

 

I think the only thing that deserves any explanation is that the default BaseClass, if one is not provided, is Microsoft.Build.Utilities.Task.  I choose to implement this in code, instead of in the XSD file because it allows me to validate at compile time instead of catching a problem at runtime.

 

So that is a basic implementation of the ITask interface.

 

Tomorrow we’ll look at the ITaskProperty interface.  It has a bit more to it.

Comments