mardi 26 janvier 2016

Create an object with method using c# reflection

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.





Aucun commentaire:

Enregistrer un commentaire