【发布时间】:2023-03-19 09:29:01
【问题描述】:
我有一个问题,我需要在 c# 中加载 dll 后使用反射创建一个 tfs 版本控制服务器对象。我在反射中初始化它时遇到了麻烦,但是因为它没有构造函数。如果没有反射,您通常使用团队项目集合对象中的 getService 方法创建对象。这是我的代码:
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 ?
}
}
}
有什么方法可以使用团队项目集合类中的 getService 方法创建此版本控制服务器对象?
任何帮助将不胜感激。
【问题讨论】:
标签: c# reflection