protected virtual void InitializeInstanceGuid()
{
if (this.projectInstanceGuid != Guid.Empty)
{
return;
}
Guid instanceGuid = Guid.Empty;
Debug.Assert(this.nestedHierarchy != null, "The nested hierarchy object must be created before calling this method");
// This method should be called from the open children method, then we can safely use the IsNewProject property
if (this.ProjectManager.IsNewProject)
{
instanceGuid = Guid.NewGuid();
this.ItemNode.SetMetadata(ProjectFileConstants.InstanceGuid, instanceGuid.ToString("B"));
ErrorHandler.ThrowOnFailure(this.nestedHierarchy.SetGuidProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, ref instanceGuid));
}
else
{
// Get a guid from the nested hierarchy.
Guid nestedHiererachyInstanceGuid;
ErrorHandler.ThrowOnFailure(this.nestedHierarchy.GetGuidProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, out nestedHiererachyInstanceGuid));
// Get instance guid from the project file. If it does not exist then we create one.
string instanceGuidAsString = this.ItemNode.GetMetadata(ProjectFileConstants.InstanceGuid);
// 1. nestedHiererachyInstanceGuid is empty and instanceGuidAsString is empty then create a new one.
// 2. nestedHiererachyInstanceGuid is empty and instanceGuidAsString not empty use instanceGuidAsString and update the nested project object by calling SetGuidProperty.
// 3. nestedHiererachyInstanceGuid is not empty instanceGuidAsString is empty then use nestedHiererachyInstanceGuid and update the outer project element.
// 4. nestedHiererachyInstanceGuid is not empty instanceGuidAsString is empty then use nestedHiererachyInstanceGuid and update the outer project element.
if (nestedHiererachyInstanceGuid == Guid.Empty && String.IsNullOrEmpty(instanceGuidAsString))
{
instanceGuid = Guid.NewGuid();
}
else if (nestedHiererachyInstanceGuid == Guid.Empty && !String.IsNullOrEmpty(instanceGuidAsString))
{
instanceGuid = new Guid(instanceGuidAsString);
ErrorHandler.ThrowOnFailure(this.nestedHierarchy.SetGuidProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, ref instanceGuid));
}
else if (nestedHiererachyInstanceGuid != Guid.Empty)
{
instanceGuid = nestedHiererachyInstanceGuid;
// If the instanceGuidAsString is empty then creating a guid out of it would throw an exception.
if (String.IsNullOrEmpty(instanceGuidAsString) || nestedHiererachyInstanceGuid != new Guid(instanceGuidAsString))
{
this.ItemNode.SetMetadata(ProjectFileConstants.InstanceGuid, instanceGuid.ToString("B"));
}
}
}
this.projectInstanceGuid = instanceGuid;
}