} else {
ctx.il.Emit (OpCodes.Ldarg_1);//load currentInterface
ctx.il.Emit (OpCodes.Ldstr, templatePath); //Load template path string
- ctx.il.Emit (OpCodes.Callvirt,//call Interface.Load(path)
- CompilerServices.miIFaceLoad);
+ //get declaring type for search fallback assembly
+ ctx.il.Emit (OpCodes.Ldloc_0);
+ ctx.il.Emit (OpCodes.Call, CompilerServices.miGetType);
+ ctx.il.Emit (OpCodes.Callvirt,
+ CompilerServices.miIFaceCreateTemplateInst);
}
ctx.il.Emit (OpCodes.Callvirt, CompilerServices.miLoadTmp);//load template
}
if (iface.ItemTemplates.ContainsKey (itemTemplatePath)) {
itemTemplateIds.Add (new string [] { "default", itemTemplatePath, "" });
} else {
- using (Stream stream = iface.GetStreamFromPath (itemTemplatePath)) {
+ using (Stream stream = iface.GetTemplateStreamFromPath (itemTemplatePath, ctx.CurrentNodeType)) {
//itemtemplate files may have multiple root nodes
XmlReaderSettings itrSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
using (XmlReader itr = XmlReader.Create (stream, itrSettings)) {
/// on the first instance creation of a IML item.
/// </summary>
public Dictionary<String, Instantiator> Instantiators = new Dictionary<string, Instantiator>();
+ public Dictionary<String, Instantiator> Templates = new Dictionary<string, Instantiator> ();
/// <summary>
/// default templates dic by metadata token
/// </summary>
#region Load/Save
+ /// <summary>get template stream from path providing the declaring type for which
+ /// this template is loaded. If not found in entry assembly, the assembly where the type is defined
+ /// will be searched
+ /// </summary>
+ /// <returns>The template stream</returns>
+ public virtual Stream GetTemplateStreamFromPath (string path, Type declaringType)
+ {
+ Stream s = null;
+ if (path.StartsWith ("#", StringComparison.Ordinal)) {
+ string resId = path.Substring (1);
+ s = Assembly.GetEntryAssembly ().GetManifestResourceStream (resId);
+ if (s == null)
+ s = Assembly.GetAssembly (declaringType).GetManifestResourceStream (resId);
+ if (s == null)
+ throw new Exception ($"Template not found '{path}'");
+ } else {
+ if (!File.Exists (path))
+ throw new FileNotFoundException ("Template not found: ", path);
+ s = new FileStream (path, FileMode.Open, FileAccess.Read);
+ }
+ return s;
+ }
/// <summary>Open file or find a resource from path string</summary>
/// <returns>A file or resource stream</returns>
/// <param name="path">This could be a normal file path, or an embedded ressource ID
if (path.StartsWith ("#", StringComparison.Ordinal)) {
string resId = path.Substring (1);
- //try/catch added to prevent nunit error
- try {
- stream = System.Reflection.Assembly.GetEntryAssembly ().GetManifestResourceStream (resId);
- } catch {}
- if (stream == null)//try to find ressource in Crow assembly
- stream = System.Reflection.Assembly.GetExecutingAssembly ().GetManifestResourceStream (resId);
+ string assemblyName = resId.Split ('.') [0];
+ Assembly a = AppDomain.CurrentDomain.GetAssemblies ().FirstOrDefault (aa => aa.GetName ().Name == assemblyName);
+ if (a == null)
+ throw new Exception ($"Assembly '{assemblyName}' not found for ressource '{path}'.");
+ stream = a.GetManifestResourceStream (resId);
if (stream == null)
throw new Exception ("Resource not found: " + path);
} else {
if (path.StartsWith ("#", StringComparison.Ordinal)) {
string resId = path.Substring (1);
- //try/catch added to prevent nunit error
- stream = Assembly.GetEntryAssembly ().GetManifestResourceStream (resId);
- if (stream == null)//try to find ressource in Crow assembly
- stream = Assembly.GetExecutingAssembly ().GetManifestResourceStream (resId);
- if (stream == null)
- throw new Exception("Resource not found: " + path);
- Console.WriteLine(Assembly.GetEntryAssembly ().GetName ());
+ string assemblyName = resId.Split ('.') [0];
+ Assembly a = AppDomain.CurrentDomain.GetAssemblies ().FirstOrDefault (aa => aa.GetName ().Name == assemblyName);
+ if (a == null)
+ throw new Exception ($"Assembly '{assemblyName}' not found for ressource '{path}'.");
+ stream = a.GetManifestResourceStream (resId);
+ /*foreach (var s in a.GetManifestResourceNames()) {
+ Console.WriteLine (s);
+ }*/
+ if (stream == null)
+ throw new Exception ("Resource not found: " + path);
} else {
if (!File.Exists (path))
throw new FileNotFoundException ("File not found: ", path);
}
}
/// <summary>
+ /// Create an instance of a GraphicObject linked to this interface but not added to the GraphicTree
+ /// </summary>
+ /// <returns>new instance of graphic object created</returns>
+ /// <param name="path">path of the iml file to load</param>
+ public virtual GraphicObject CreateTemplateInstance (string path, Type declaringType)
+ {
+ try {
+ if (!Templates.ContainsKey (path))
+ Templates [path] = new Instantiator (this, GetTemplateStreamFromPath(path, declaringType), path);
+ return Templates [path].CreateInstance ();
+ } catch (Exception ex) {
+ throw new Exception ("Error loading Template <" + path + ">:", ex);
+ }
+ }
+ /// <summary>
/// Fetch instantiator from cache or create it.
/// </summary>
/// <returns>new Instantiator</returns>
string fetchMethodName;
string dataTest;
- static Stream getItemTemplateStream (string path, Type declaringType)
- {
- Stream s = null;
- if (path.StartsWith ("#", StringComparison.Ordinal)) {
- string resId = path.Substring (1);
- s = Assembly.GetEntryAssembly ().GetManifestResourceStream (resId);
- if (s == null)
- s = Assembly.GetAssembly (declaringType).GetManifestResourceStream (resId);
- if (s == null)
- throw new Exception ($"Item Template not found '{path}'");
- } else {
- if (!File.Exists (path))
- throw new FileNotFoundException ("Item Template not found: ", path);
- s = new FileStream (path, FileMode.Open, FileAccess.Read);
- }
- return s;
- }
-
#region CTOR
/// <summary>
/// Initializes a new instance of the <see cref="Crow.ItemTemplate"/> class by parsing the file passed as argument.
/// <param name="_dataType">type this item will be choosen for, or member of the data item</param>
/// <param name="_fetchDataMethod">for hierarchical data, method to call for children fetching</param>
public ItemTemplate (Interface _iface, string path, Type declaringType, string _dataTest = "TypeOf", string _dataType = "default", string _fetchDataMethod = null)
- : base(_iface, getItemTemplateStream(path, declaringType)) {
+ : base(_iface, _iface.GetTemplateStreamFromPath (path, declaringType)) {
strDataType = _dataType;
fetchMethodName = _fetchDataMethod;
dataTest = _dataTest;