Reputation: 60
I'd like to check and update csproj file if a specified item in csproj is updated.
DocumentSaved event is not fired when csproj file is saved. So I want to handle project reload event or csproj file save event.
Does anyone has in idea for it?
postscript:
To be specific, I'd like to rewrite HintPath with SolutionDir property ($(SolutionDir)\packages\.. ) instead of relative path(..\packages\.. ) after HintPath is added or updated via Nuget.
The answer by Matze works fine!
Upvotes: 1
Views: 288
Reputation: 5508
The documentation of the _dispDocumentEvents_Event
interface says...
This API supports the product infrastructure and is not intended to be used directly from your code.
Microsoft Internal Use Only.
Of course, the interface is public, but the usage is rarely documented and it´s availability and function might change or could be removed in a future version of the IDE. I would not recommend to use it as a foundation for custom extension functionality.
Instead you could use the IVsRunningDocumentTable
service and a custom implementation of the IVsRunningDocTableEvents
interface, which provides similiar functionality by the AfterSave
event. I would suggest an implementation of a class that handles subscription to events and hides unnecessary details. For instance...
internal abstract class RunningDocumentTableEvents :
IDisposable,
IVsRunningDocTableEvents
{
private readonly IVsRunningDocumentTable rdt;
private readonly uint sinkCookie;
public RunningDocumentTableEvents(IServiceProvider serviceProvider)
{
this.rdt = serviceProvider.GetSerice(typeof(SVsRunningDocumentTable))
as IVsRunningDocumentTable;
uint cookie;
this.rdt.AdviseRunningDocTableEvents(this, out cookie);
this.sinkCookie = cookie;
}
protected abstract void OnAfterSave(AfterSaveEventArgs e);
int IVsRunningDocTableEvents.OnAfterSave(uint docCookie)
{
uint flags, readLocks, editLocks, itemId;
string moniker;
IVsHierarchy hierarchy;
IntPtr docData;
int hr = this.rdt.GetDocumentInfo(
docCookie, out flags, out readLocks, out editLocks, out moniker,
out hierarchy, out itemId, out docData);
if (hr == VSConstants.S_OK)
{
var e = new AfterSaveEventArgs { FileName = moniker, ... };
this.OnAfterSave(e);
}
return VSConstants.S_OK;
}
...
public void Dispose()
{
this.rdt.UnadviseRunningDocTableEvents(this.sinkCookie);
}
}
The OnAfterSave
callback receives a cookie which can be passed to the GetDocumentInfo
method of an IVsRunningDocumentTable
object in order to obtain the document´s filename, locks, the related hierarchy item as well as a pointer to an IVsTextBuffer
that holds the documents data.
Upvotes: 1