BuildingCoder.CmdProjectParameterGuids.AddProjectParameterBinding C# (CSharp) Method

AddProjectParameterBinding() static private method

This method takes a category and information about a project parameter and adds a binding to the category for the parameter. It will throw an exception if the parameter is already bound to the desired category. It returns whether or not the API reports that it successfully bound the parameter to the desired category.
static private AddProjectParameterBinding ( Document doc, ProjectParameterData projectParameterData, Category category ) : bool
doc Document The project document in which the project parameter has been defined
projectParameterData ProjectParameterData Information about the project parameter
category Category The additional category to which to bind the project parameter
return bool
        static bool AddProjectParameterBinding(
            Document doc,
            ProjectParameterData projectParameterData,
            Category category)
        {
            // Following good SOA practices, first validate incoming parameters

              if( doc == null )
              {
            throw new ArgumentNullException( "doc" );
              }

              if( doc.IsFamilyDocument )
              {
            throw new Exception(
              "doc can not be a family document." );
              }

              if( projectParameterData == null )
              {
            throw new ArgumentNullException(
              "projectParameterData" );
              }

              if( category == null )
              {
            throw new ArgumentNullException( "category" );
              }

              bool result = false;

              CategorySet cats = projectParameterData.Binding
            .Categories;

              if( cats.Contains( category ) )
              {
            // It's already bound to the desired category.
            // Nothing to do.
            string errorMessage = string.Format(
              "The project parameter '{0}' is already bound to the '{1}' category.",
              projectParameterData.Definition.Name,
              category.Name );

            throw new Exception( errorMessage );
              }

              cats.Insert( category );

              // See if the parameter is an instance or type parameter.

              InstanceBinding instanceBinding
            = projectParameterData.Binding as InstanceBinding;

              if( instanceBinding != null )
              {
            // Is an Instance parameter

            InstanceBinding newInstanceBinding
              = doc.Application.Create
            .NewInstanceBinding( cats );

            if( doc.ParameterBindings.ReInsert(
              projectParameterData.Definition,
              newInstanceBinding ) )
            {
              result = true;
            }
              }
              else
              {
            // Is a type parameter
            TypeBinding typeBinding
              = doc.Application.Create
            .NewTypeBinding( cats );

            if( doc.ParameterBindings.ReInsert(
              projectParameterData.Definition, typeBinding ) )
            {
              result = true;
            }
              }
              return result;
        }