I have a problem where I need to create a tfs version control server object using reflection after loading the dll in c#. I'm having trouble initializing it in reflection however as it has no constructors. Without reflection you normally create the object using the getService method in a team project collection object. Here is my code:
namespace SendFiletoTFS
{
class Program
{
static void Main(string[] args)
{
String tfsuri = @"uri";
NetworkCredential cred = new NetworkCredential("user", "password", "domain");
// Load in the assemblies Microsoft.TeamFoundation.Client.dll and Microsoft.TeamFoundation.VersionControl.Client.dll
Assembly tfsclient = Assembly.LoadFrom(@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Client.dll");
Assembly versioncontrol = Assembly.LoadFrom(@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.VersionControl.Client.dll");
// Create Team Project Collection
Type tpcclass = tfsclient.GetType(@"Microsoft.TeamFoundation.Client.TfsTeamProjectCollection");
// The 'getService' method.
MethodInfo getService = tpcclass.GetMethods()[32];
object tpc = Activator.CreateInstance(tpcclass, new object[] { new Uri(tfsuri), cred });
Type VersionControlServerClass = versioncontrol.GetType(@"Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer");
// Code I'm trying to emulate in reflection, this is how I would normally do it without reflection.
//VersionControlServer versionControl = tpc.GetService<VersionControlServer>();
// Create VersionControlServer Class. This line will not work and give a no constructor found exception.
object vcs = Activator.CreateInstance(VersionControlServerClass, new object[] { tpc });
//How do I create the vcs object ?
}
}
}
Is there some way I can create this version control server object using the getService method in the team project collection class?
Any help would be greatly appreciated.
You can call the method this way:
var closedMethod = getService.MakeGenericMethod(VersionControlServerClass);
object vcs = closedMethod.Invoke(tpc, null);
As a note, you should not use something like tpcclass.GetMethods()[32];
because reflection does not guarantee you the order of the returned methods. Better use GetMethod([methodname]);
注意, TfsTeamProjectCollection
实现了IServiceProvider
,它实际上具有GetService的非通用版本:
object vcs = ((IServiceProvider)tpc).GetService(VersionControlServerClass);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.