📄 abstractproject.cs
字号:
!Path.GetDirectoryName(file).EndsWith("ProjectDocumentation")) {
newFiles.Add(file);
}
}
if (newFiles.Count > 0) {
if (newFileSearch == NewFileSearch.OnLoadAutoInsert) {
foreach (string file in newFiles) {
ProjectFile newFile = new ProjectFile(file);
newFile.BuildAction = IsCompileable(file) ? BuildAction.Compile : BuildAction.Nothing;
projectFiles.Add(newFile);
}
} else {
new IncludeFilesDialog(this, newFiles).ShowDialog();
}
}
}
public virtual void LoadProject(string fileName)
{
basedirectory = Path.GetDirectoryName(fileName);
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
string version = null;
if (doc.DocumentElement.Attributes["version"] == null) {
if (doc.DocumentElement.Attributes["fileversion"] != null) {
version = doc.DocumentElement.Attributes["fileversion"].InnerText;
}
} else {
version = doc.DocumentElement.Attributes["version"].InnerText;
}
if (version != "1.0" && version != currentProjectFileVersion) {
throw new UnknownProjectVersionException(version);
}
if (version == "1.0") {
string tempFile = Path.GetTempFileName();
IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
messageService.ShowMessage("Old project file format found.\n It will be automatically converted to " + currentProjectFileVersion,
"Information");
PropertyService propertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));
ConvertXml.Convert(fileName,
propertyService.DataDirectory + Path.DirectorySeparatorChar +
"ConversionStyleSheets" + Path.DirectorySeparatorChar +
"ConvertPrjx10to11.xsl",
tempFile);
try {
File.Delete(fileName);
File.Copy(tempFile, fileName);
LoadProject(fileName);
File.Delete(tempFile);
return;
} catch (Exception) {
messageService.ShowError("Error writing the old project file.\nCheck if you have write permission on the project file (.prjx).\n A non persistent proxy project will be created but no changes will be saved.\nIt is better if you close SharpDevelop and correct the problem.");
if (File.Exists(tempFile)) {
doc.Load(tempFile);
File.Delete(tempFile);
} else {
messageService.ShowError("Can't remove temp file. (should never happen)");
}
}
}
GetXmlAttributes(doc, doc.DocumentElement, this);
// add the configurations
XmlNode configurationElement = doc.DocumentElement.SelectSingleNode("Configurations");
string activeConfigurationName = configurationElement.Attributes["active"].InnerText;
foreach (XmlNode configuration in configurationElement.ChildNodes) {
if (configuration.Name == configurationNodeName) {
IConfiguration newConfiguration = CreateConfiguration();
GetXmlAttributes(doc, (XmlElement)configuration, newConfiguration);
if (newConfiguration.Name == activeConfigurationName) {
activeConfiguration = newConfiguration;
}
Configurations.Add(newConfiguration);
}
}
SearchNewFiles();
}
void GetXmlAttributes(XmlDocument doc, XmlElement element, object o)
{
FieldInfo[] fieldInfos = o.GetType().GetFields(BindingFlags.FlattenHierarchy |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.NonPublic);
foreach (FieldInfo fieldInfo in fieldInfos) {
// set the xml attributes for this object
XmlAttributeAttribute[] xmlAttributes = (XmlAttributeAttribute[])fieldInfo.GetCustomAttributes(typeof(XmlAttributeAttribute), true);
ConvertToRelativePathAttribute[] convertToRelPath = (ConvertToRelativePathAttribute[])fieldInfo.GetCustomAttributes(typeof(ConvertToRelativePathAttribute), true);
bool convertRel = convertToRelPath != null && convertToRelPath.Length > 0;
if (xmlAttributes != null && xmlAttributes.Length > 0) {
try {
XmlAttribute xmlAttribute = element.Attributes[xmlAttributes[0].Name];
if (xmlAttribute != null) {
if (convertRel && convertToRelPath[0].PredicatePropertyName != null && convertToRelPath[0].PredicatePropertyName.Length > 0) {
PropertyInfo myPropInfo = o.GetType().GetProperty(convertToRelPath[0].PredicatePropertyName,
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance);
if (myPropInfo != null) {
convertRel = (bool)myPropInfo.GetValue(o, null);
}
}
string val = null;
if (convertRel) {
if (xmlAttribute.InnerText.Length == 0) {
val = String.Empty;
} else {
string fileName = xmlAttribute.InnerText;
fileName = fileName.Replace('\\', Path.DirectorySeparatorChar);
fileName = fileName.Replace('/', Path.DirectorySeparatorChar);
val = fileUtilityService.RelativeToAbsolutePath(basedirectory, fileName);
string command = xmlAttribute.InnerText;
// string arguments = "";
// int idx = command.IndexOf(' ');
// if (idx > 0) {
// arguments = command.Substring(idx + 1);
// command = command.Substring(0, idx);
// }
//
// val = fileUtilityService.RelativeToAbsolutePath(basedirectory, command);
// if (arguments.Length > 0) {
// val += " " + arguments;
// }
}
} else {
val = xmlAttribute.InnerText;
}
if (val != null) {
if (fieldInfo.FieldType.IsEnum) {
fieldInfo.SetValue(o, Enum.Parse(fieldInfo.FieldType,val));
} else {
fieldInfo.SetValue(o, Convert.ChangeType(val, fieldInfo.FieldType));
}
}
}
} catch {
IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
messageService.ShowMessage(fieldInfo.Name + " -- error");
throw;
}
} else { // add sets to the xmlElement
XmlSetAttribute[] xmlSetAttributes = (XmlSetAttribute[])fieldInfo.GetCustomAttributes(typeof(XmlSetAttribute), true);
if (xmlSetAttributes != null && xmlSetAttributes.Length > 0) {
XmlElement setElement;
if (xmlSetAttributes[0].Name == null) {
setElement = element;
} else {
setElement = (XmlElement)element.SelectSingleNode("descendant::" + xmlSetAttributes[0].Name);
}
IList collection = (IList)fieldInfo.GetValue(o);
foreach (XmlNode childNode in setElement.ChildNodes) {
object instance = xmlSetAttributes[0].Type.Assembly.CreateInstance(xmlSetAttributes[0].Type.FullName);
GetXmlAttributes(doc, (XmlElement)childNode, instance);
collection.Add(instance);
}
} else { // finally try, if the field is from a type which has a XmlNodeName attribute attached
XmlNodeNameAttribute[] xmlNodeNames = (XmlNodeNameAttribute[])fieldInfo.FieldType.GetCustomAttributes(typeof(XmlNodeNameAttribute), true);
if (xmlNodeNames != null && xmlNodeNames.Length == 1) {
XmlElement el = (XmlElement)element.SelectSingleNode("descendant::" + xmlNodeNames[0].Name);
object instance = fieldInfo.FieldType.Assembly.CreateInstance(fieldInfo.FieldType.FullName);
if (el != null) {
GetXmlAttributes(doc, el, instance);
}
fieldInfo.SetValue(o, instance);
}
}
}
}
}
void SetXmlAttributes(XmlDocument doc, XmlElement element, object o)
{
FieldInfo[] fieldInfos = o.GetType().GetFields(BindingFlags.FlattenHierarchy |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.NonPublic);
foreach (FieldInfo fieldInfo in fieldInfos) {
// set the xml attributes for this object
XmlAttributeAttribute[] xmlAttributes = (XmlAttributeAttribute[])fieldInfo.GetCustomAttributes(typeof(XmlAttributeAttribute), true);
ConvertToRelativePathAttribute[] convertToRelPath = (ConvertToRelativePathAttribute[])fieldInfo.GetCustomAttributes(typeof(ConvertToRelativePathAttribute), true);
bool convertRel = convertToRelPath != null && convertToRelPath.Length > 0;
if (xmlAttributes != null && xmlAttributes.Length > 0) {
XmlAttribute xmlAttribute = doc.CreateAttribute(xmlAttributes[0].Name);
object fieldValue = fieldInfo.GetValue(o);
if (convertRel && convertToRelPath[0].PredicatePropertyName != null && convertToRelPath[0].PredicatePropertyName.Length > 0) {
PropertyInfo myPropInfo = o.GetType().GetProperty(convertToRelPath[0].PredicatePropertyName,
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance);
if (myPropInfo != null) {
convertRel = (bool)myPropInfo.GetValue(o, null);
}
}
if (convertRel) {
string val = fieldValue == null ? String.Empty : fieldValue.ToString();
if (val.Length == 0) {
fieldValue = String.Empty;
} else {
fieldValue = fileUtilityService.AbsoluteToRelativePath(basedirectory, val);
// string command = val;
// string arguments = "";
// int idx = command.IndexOf(' ');
// if (idx > 0) {
// arguments = command.Substring(idx + 1);
// command = command.Substring(0, idx);
// }
//
// fieldValue = fileUtilityService.AbsoluteToRelativePath(basedirectory, command);
// if (arguments.Length > 0) {
// fieldValue += " " + arguments;
// }
}
}
xmlAttribute.InnerText = fieldValue == null ? String.Empty : fieldValue.ToString();
element.Attributes.Append(xmlAttribute);
} else { // add sets to the xmlElement
XmlSetAttribute[] xmlSetAttributes = (XmlSetAttribute[])fieldInfo.GetCustomAttributes(typeof(XmlSetAttribute), true);
if (xmlSetAttributes != null && xmlSetAttributes.Length > 0) {
XmlElement setElement;
if (xmlSetAttributes[0].Name == null) {
setElement = element;
} else {
setElement = doc.CreateElement(xmlSetAttributes[0].Name);
}
try {
// A set must always be a collection
ICollection collection = (ICollection)fieldInfo.GetValue(o);
if (collection != null) {
foreach (object collectionObject in collection) {
XmlNodeNameAttribute[] xmlNodeNames = (XmlNodeNameAttribute[])collectionObject.GetType().GetCustomAttributes(typeof(XmlNodeNameAttribute), true);
if (xmlNodeNames == null || xmlNodeNames.Length != 1) {
throw new Exception("XmlNodeNames mismatch");
}
XmlElement collectionElement = doc.CreateElement(xmlNodeNames[0].Name);
SetXmlAttributes(doc, collectionElement, collectionObject);
setElement.AppendChild(collectionElement);
}
}
if (element != setElement) {
element.AppendChild(setElement);
}
} catch {
IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
messageService.ShowMessage(xmlSetAttributes[0].Name + " -- error");
throw;
}
} else { // finally try, if the field is from a type which has a XmlNodeName attribute attached
object fieldValue = fieldInfo.GetValue(o);
if (fieldValue != null) {
XmlNodeNameAttribute[] xmlNodeNames = (XmlNodeNameAttribute[])fieldValue.GetType().GetCustomAttributes(typeof(XmlNodeNameAttribute), true);
if (xmlNodeNames != null && xmlNodeNames.Length == 1) {
XmlElement setElement = doc.CreateElement(xmlNodeNames[0].Name);
SetXmlAttributes(doc, setElement, fieldValue);
element.AppendChild(setElement);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -