Accepted answer

i think you're going to have to simply pass the datacontext to the code (manually). sorry.


yes - using reflection is about the only way to determine the datacontext to which the query belongs. it's the same with the data objects that are created when the query is triggered.

what follows doesn't strictly answer rune's question, but may be helpful if you want to use reflection to determine whether a data object as attached to and monitored by a data context:

the following code defines a context property which can be placed onto a data object, and then used to return the datacontext (if any) that the object is attached to.

private const standardchangetrackername as string = ""

private _context as dataclasses1datacontext
public property context() as dataclasses1datacontext
        dim hascontext as boolean = false
        dim mytype as type = me.gettype()
        dim propertychangingfield as fieldinfo = mytype.getfield("propertychangingevent", bindingflags.nonpublic or bindingflags.instance)
        dim propertychangingdelegate as propertychangingeventhandler = propertychangingfield.getvalue(me)
        dim delegatetype as type = nothing

        for each thisdelegate in propertychangingdelegate.getinvocationlist()
            delegatetype =
            if delegatetype.fullname.equals(standardchangetrackername) then
                propertychangingdelegate = thisdelegate
                hascontext = true
                exit for
            end if

        if hascontext then
            dim targetfield =
            dim servicesfield as fieldinfo = targetfield.gettype().getfield("services", bindingflags.nonpublic or bindingflags.instance)
            if servicesfield isnot nothing then

                dim servicesobject = servicesfield.getvalue(targetfield)

                dim contextfield as fieldinfo = servicesobject.gettype.getfield("context", bindingflags.nonpublic or bindingflags.instance)

                _context = contextfield.getvalue(servicesobject)

            end if
        end if
        return _context
    end get
    set(byval value as dataclasses1datacontext)

        _context = value

    end set

end property

take care to note that the object can only locate it's datacontext if it is currently attached to the context with changetracking switched on. this property relies on the fact that the datacontext has subscribed to the object's onpropertychanging event to monitor changes over the lifespan of the object.

if this was helpful, please up-vote this post.

for more info on using reflection to find event handlers:

Related Query

More Query from same tag