Proje öğesine öznitelik ekleme

Yöntemleri GetItemAttribute ve SetItemAttribute bir proje öğesinin özniteliklerinin değerini alır ve ayarlar. SetItemAttribute, henüz yoksa özniteliği oluşturur ve bunu proje öğesi meta verilerine ekler.

Proje öğesine öznitelik ekleme

  • Aşağıdaki kod, otomasyon nesnesini ve SetItemAttribute yöntemini kullanarak DTE bir proje öğesine öznitelik ekler. Proje öğesi kimliği, "program.cs" proje öğesi adından alınır. "MyAttribute" özniteliği bu proje öğesine eklenir ve "MyValue" değeri verilir.

    EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
    EnvDTE.Project project = dte.Solution.Projects.Item(1);
    
    string uniqueName = project.UniqueName;
    IVsSolution solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));
    IVsHierarchy hierarchy;
    solution.GetProjectOfUniqueName(uniqueName, out hierarchy);
    IVsBuildPropertyStorage buildPropertyStorage = hierarchy as IVsBuildPropertyStorage;
    if (buildPropertyStorage != null)
    {
        uint itemId;
        string fullPath = (string)project.ProjectItems.Item("Program.cs").Properties.Item("FullPath").Value;
        hierarchy.ParseCanonicalName(fullPath, out itemId);
        buildPropertyStorage.SetItemAttribute(itemId, "MyAttribute", "MyValue");
    }