22
22
using System.Collections;
23
using System.Collections.Generic;
24
25
using System.Diagnostics;
26
27
using MonoDevelop.Core;
27
28
using System.ComponentModel;
28
29
using MonoDevelop.Projects;
29
using MonoDevelop.Projects.Serialization;
30
using MonoDevelop.Core.Serialization;
31
32
namespace MonoDevelop.Projects
41
42
/// This class represent a reference information in an Project object.
43
44
[DataItem (FallbackType=typeof(UnknownProjectReference))]
44
public class ProjectReference : ICloneable, ICustomDataItem
45
public class ProjectReference : ICloneable, IExtendedDataItem
46
[ItemProperty ("type")]
47
Hashtable extendedProperties;
47
48
ReferenceType referenceType;
49
DotNetProject ownerProject;
51
50
string reference = String.Empty;
51
bool localCopy = true;
53
53
// A project may reference assemblies which are not available
54
54
// in the system where it is opened. For example, opening
58
58
// The loadedReference stores the reference initially loaded,
59
59
// so it can be saved again.
60
60
string loadedReference;
62
[ItemProperty ("localcopy")]
63
bool localCopy = true;
61
bool specificVersion = true;
65
64
public ProjectReference ()
69
internal void SetOwnerProject (Project project)
68
internal void SetOwnerProject (DotNetProject project)
71
70
ownerProject = project;
71
UpdateGacReference ();
74
74
public ProjectReference(ReferenceType referenceType, string reference)
76
if (referenceType == ReferenceType.Assembly)
77
specificVersion = false;
76
78
this.referenceType = referenceType;
77
79
this.reference = reference;
78
80
UpdateGacReference ();
84
86
reference = referencedProject.Name;
89
public IDictionary ExtendedProperties {
91
if (extendedProperties == null)
92
extendedProperties = new Hashtable ();
93
return extendedProperties;
97
public Project OwnerProject {
98
get { return ownerProject; }
88
101
public ReferenceType ReferenceType {
90
103
return referenceType;
95
107
public string Reference {
113
UpdateGacReference ();
102
117
public string StoredReference {
104
119
if (loadedReference != null)
117
131
localCopy = value;
135
internal string LoadedReference {
137
return loadedReference;
140
loadedReference = value;
144
internal string Condition { get; set; }
146
public bool SpecificVersion {
148
return specificVersion;
151
specificVersion = value;
155
public bool IsValid {
157
if (ReferenceType == ReferenceType.Gac) {
160
if (!IsExactVersion && SpecificVersion)
162
} else if (ReferenceType == ReferenceType.Project) {
163
if (ownerProject != null && ownerProject.ParentSolution != null) {
164
DotNetProject p = ownerProject.ParentSolution.FindProjectByName (reference) as DotNetProject;
166
return ownerProject.TargetFramework.IsCompatibleWithFramework (p.TargetFramework.Id);
173
public bool IsExactVersion {
175
if (ReferenceType == ReferenceType.Gac) {
176
if (StoredReference != Reference && !Reference.StartsWith (StoredReference + ","))
122
184
/// Returns the file name to an assembly, regardless of what
123
185
/// type the assembly is.
125
string GetReferencedFileName ()
187
string GetReferencedFileName (string configuration)
127
189
switch (ReferenceType) {
128
190
case ReferenceType.Assembly:
133
195
return file == null ? reference : file;
134
196
case ReferenceType.Project:
135
197
if (ownerProject != null) {
136
Combine c = ownerProject.RootCombine;
138
Project p = c.FindProject (reference);
139
if (p != null) return p.GetOutputFileName ();
198
if (ownerProject.ParentSolution != null) {
199
Project p = ownerProject.ParentSolution.FindProjectByName (reference);
200
if (p != null) return p.GetOutputFileName (configuration);
149
public virtual string[] GetReferencedFileNames ()
210
public virtual string[] GetReferencedFileNames (string configuration)
151
string s = GetReferencedFileName ();
212
string s = GetReferencedFileName (configuration);
153
214
return new string[] { s };
155
216
return new string [0];
158
DataCollection ICustomDataItem.Serialize (ITypeSerializer handler)
160
DataCollection data = handler.Serialize (this);
161
string refto = reference;
162
if (referenceType == ReferenceType.Assembly) {
163
string basePath = Path.GetDirectoryName (handler.SerializationContext.BaseFile);
164
refto = FileService.AbsoluteToRelativePath (basePath, refto);
165
} else if (referenceType == ReferenceType.Gac && loadedReference != null)
166
refto = loadedReference;
168
data.Add (new DataValue ("refto", refto));
172
void ICustomDataItem.Deserialize (ITypeSerializer handler, DataCollection data)
174
DataValue refto = data.Extract ("refto") as DataValue;
175
handler.Deserialize (this, data);
177
reference = refto.Value;
178
UpdateGacReference ();
179
if (referenceType == ReferenceType.Assembly) {
180
string basePath = Path.GetDirectoryName (handler.SerializationContext.BaseFile);
181
reference = FileService.RelativeToAbsolutePath (basePath, reference);
186
219
void UpdateGacReference ()
188
221
if (referenceType == ReferenceType.Gac) {
189
223
string cref = Runtime.SystemAssemblyService.FindInstalledAssembly (reference);
224
if (ownerProject != null) {
227
cref = Runtime.SystemAssemblyService.GetAssemblyNameForVersion (cref, ownerProject.TargetFramework);
228
notFound = (cref == null);
190
230
if (cref != null && cref != reference) {
191
loadedReference = reference;
231
if (loadedReference == null) {
232
loadedReference = reference;
192
234
reference = cref;
194
loadedReference = null;