C# using others code

2019-01-09 15:34发布

Iv'e downloaded a C# interval tree collection class class from here http://intervaltree.codeplex.com/SourceControl/list/changesets -> Right hand side -> Download.

However I can't open the whole project on my Microsoft Visual C# 2010 Express (that also runs C# XNA) because

Solution folders are not supported in this version of the application

Also I just want the class to use separately in my own seprate project.

I tried to copy the three important seeming files Interval.cs, IntervalNode.cs and IntervalTree.cs into my project but this generated the compile error

There are no importers which handle this file type

I've also tried to copy and paste the contents of the three files into my project, encapsulating them into there own namespace as well as there was a lot of code. I had to rearange some of the usings a little but have run into the problem that possibly it wants PowerCollections .dll and .pcb files as using Wintellect.PowerCollections; causes

The type or namespace name 'Wintellect' could not be found (are you missing a using directive or an assembly reference?)

I'm not sure how to continue or if I'm doing the right thing at all in how to get this class to work.

3条回答
劫难
2楼-- · 2019-01-09 15:48

Open the IntervalTreeLib.csproj file if you want to be able to open the project in it's entirety (or in your current solution add an existing project (you can right-click on the solution) and select the IntervalTreeLib.csproj). If you are trying to grab just the code file in your project, ensure you also grab the PowerCollections.dll file (I see it is in the same folder as the code files) or your code will not compile (as you have discovered). You'll need to add a reference to it and include the needed using statement at the top of the code files making use of this library (or use fully qualified name with the namespace).

using IntervalTreeLib;

or

var myObj = new IntervalTreeLib.[WhateverClass](...);

Also, make sure you read the license.txt file. You may need to include it if you are using the code. Give credit where it is due.

UPDATE:

If the test project is causing you problems, just open the library project. Ideally you could just open that and compile it, adding the output DLL files that are generated directly into your solution. This is ideal unless you are planning on changing the library source code itself.

查看更多
Deceive 欺骗
3楼-- · 2019-01-09 15:58

Since discussing that you are able to build Intervallib.dll, we will discuss about how you should the dll in your project.

Now in your proj, right click on the references part and add the dll intervallib.dll to your references. In your game.cs file, have the reference to the namespace as -- using IntervalTreeLib;

then you should actually copy the dll powercollections.dll to the bin directory of proj directory also.

you should copy this dll because there is an indirect link to the dll as it is used in IntervalTreeLib.dll

following these steps, I was able to execute this project.

查看更多
劫难
4楼-- · 2019-01-09 16:12

Add the library to your solution

Copy the IntervalTreeLib directory into your solution directory. Then, right-click your solution, and add existing project. Point it at IntervalTreeLib.csproj in IntervalTreeLib, and click Open. That should add the IntervalTreeLib project to your solution.

Add Existing Project Screenshot

Add a reference to the library in your project

Then, in your project, add a reference to the IntervalTreeLib proejct: - Right click the References folder, and Add Reference. Click the Projects tab, and select IntervalTreeLib.

Add Reference Screenshot

Select Project Reference Screenshot

Use the classes in your code

To use classes from the library in your source then, you need to either add:

using IntervalTreeLib;

void Foo() {
    IntervalTree<int, int> tree = new ...
}

Or, refer to them by their full name:

IntervalTreeLib.IntervalTree<int, int> tree = new ...
查看更多
登录 后发表回答