Is there a way in C# to get the Assembly of the calling method? (Not the current method.)
i.e. I want the executing assembly, one above in the call stack.
Try this
Assembly.GetCallingAssembly();
How about this:
StackTrace stackTrace = new StackTrace(); // get call stack
var assembly = stackTrace.GetFrame(0).GetMethod().DeclaringType.Assembly;
With help from http://www.csharp-examples.net/reflection-callstack/
This is what I use:
var stackFrames = new StackTrace().GetFrames();
if(stackFrames==null) return null;
var executingAssembly = Assembly.GetExecutingAssembly();
foreach (var frame in stackFrames)
{
var assembly = frame.GetMethod().DeclaringType.Assembly;
if (assembly != executingAssembly)
{
return assembly;
}
}
return null;
No it's not possible to reliably understand who is calling you. Some people will undoubtedly suggest a stack walk but that is unreliable due to JIT inlining. There is just no way to reliably get the method / assembly which is calling your method.
Wrote this to get the AssemblyInfo.cs attributes for the calling assembly -- so uses the "GetAssembly(int stackTraceLevel)" method that does what is needed...
All this is also up on my blog at http://lancelarsen.com/reading-values-from-assemblyinfo-cs/
Enjoy
/// <summary>
/// Gets the values from the AssemblyInfo.cs file for the previous assembly
/// </summary>
/// <example>
/// AssemblyInfoCalling assembly = new AssemblyInfoCalling();
/// string company1 = assembly.Company;
/// string product1 = assembly.Product;
/// string copyright1 = assembly.Copyright;
/// string trademark1 = assembly.Trademark;
/// string title1 = assembly.Title;
/// string description1 = assembly.Description;
/// string configuration1 = assembly.Configuration;
/// string fileversion1 = assembly.FileVersion;
/// Version version1 = assembly.Version;
/// string versionFull1 = assembly.VersionFull;
/// string versionMajor1 = assembly.VersionMajor;
/// string versionMinor1 = assembly.VersionMinor;
/// string versionBuild1 = assembly.VersionBuild;
/// string versionRevision1 = assembly.VersionRevision;
/// </example>
public class AssemblyInfoCalling
{
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyInfoCalling"/> class.
/// </summary>
/// <param name="traceLevel">The trace level needed to get correct assembly
/// - will need to adjust based on where you put these classes in your project(s).</param>
public AssemblyInfoCalling(int traceLevel = 4)
{
//----------------------------------------------------------------------
// Default to "3" as the number of levels back in the stack trace to get the
// correct assembly for "calling" assembly
//----------------------------------------------------------------------
StackTraceLevel = traceLevel;
}
//----------------------------------------------------------------------
// Standard assembly attributes
//----------------------------------------------------------------------
public string Company { get { return GetCallingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
public string Product { get { return GetCallingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
public string Copyright { get { return GetCallingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
public string Trademark { get { return GetCallingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
public string Title { get { return GetCallingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
public string Description { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
public string Configuration { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
public string FileVersion { get { return GetCallingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }
//----------------------------------------------------------------------
// Version attributes
//----------------------------------------------------------------------
public static Version Version
{
get
{
//----------------------------------------------------------------------
// Get the assembly, return empty if null
//----------------------------------------------------------------------
Assembly assembly = GetAssembly(StackTraceLevel);
return assembly == null ? new Version() : assembly.GetName().Version;
}
}
public string VersionFull { get { return Version.ToString(); } }
public string VersionMajor { get { return Version.Major.ToString(); } }
public string VersionMinor { get { return Version.Minor.ToString(); } }
public string VersionBuild { get { return Version.Build.ToString(); } }
public string VersionRevision { get { return Version.Revision.ToString(); } }
//----------------------------------------------------------------------
// Set how deep in the stack trace we're looking - allows for customized changes
//----------------------------------------------------------------------
public static int StackTraceLevel { get; set; }
//----------------------------------------------------------------------
// Custom Attributes
//----------------------------------------------------------------------
public string Location
{
get
{
try
{
return GetCallingAssemblyAttribute<AssemblyLocationAttribute>(a => a.Value);
}
catch (NullReferenceException)
{
return string.Empty;
}
}
}
/// <summary>
/// Gets the calling assembly attribute.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <example>return GetCallingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company);</example>
/// <returns></returns>
private string GetCallingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
{
//----------------------------------------------------------------------
// Get the assembly, return empty if null
//----------------------------------------------------------------------
Assembly assembly = GetAssembly(StackTraceLevel);
if (assembly == null) return string.Empty;
//----------------------------------------------------------------------
// Get the attribute value
//----------------------------------------------------------------------
T attribute = (T) Attribute.GetCustomAttribute(assembly, typeof (T));
return value.Invoke(attribute);
}
/// <summary>
/// Go through the stack and gets the assembly
/// </summary>
/// <param name="stackTraceLevel">The stack trace level.</param>
/// <returns></returns>
private static Assembly GetAssembly(int stackTraceLevel)
{
//----------------------------------------------------------------------
// Get the stack frame, returning null if none
//----------------------------------------------------------------------
StackTrace stackTrace = new StackTrace();
StackFrame[] stackFrames = stackTrace.GetFrames();
if (stackFrames == null) return null;
//----------------------------------------------------------------------
// Get the declaring type from the associated stack frame, returning null if nonw
//----------------------------------------------------------------------
var declaringType = stackFrames[stackTraceLevel].GetMethod().DeclaringType;
if (declaringType == null) return null;
//----------------------------------------------------------------------
// Return the assembly
//----------------------------------------------------------------------
var assembly = declaringType.Assembly;
return assembly;
}
}
Came across this looking for something similar. In my case, it was Assembly.GetEntryAssembly().
Use Case
Program A runs CodeDomProvider.CompileAssemblyFromSource to embed resources and compile Program B. DLL in Program B then reads resources, but doesn't know the Assembly (which the above solves).
Related
I am keep getting
System.Reflection.TargetInvocationException
and PresentationFramework.dll, additional info Exception has been thrown by the target of an invocation.
Can someone please help me out here?
Info:
Call Stack
PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader xamlReader, System.Xaml.IXamlObjectWriterFactory writerFactory, bool skipJournaledProperties, object rootObject, System.Xaml.XamlObjectWriterSettings settings, System.Uri baseUri) Unknown
namespace PMD.Analysis.AnalysisViewModel
{
using PMD.Measurement.MeasurementModel;
using System.Windows.Data;
using PMD.Analysis.AnalysisModel;
using System;
using System.Collections.Generic;
using PMD.Measurement.MeasurementViewModel;
public class AnalysisViewModel : ViewModel
{
/// <summary>
/// New analysis command.
/// </summary>
private ICommand newAnalysis = null;
public PMD.Analysis.AnalysisViewModel.NewAnalysisViewModel m_NewAnalysisViewModel;
Measurement measurement = new Measurement();
private ICollectionView measurements = null;
/// <summary>
/// Measurement's search by title field.
/// </summary>
private string searchTitle;
/// <summary>
/// Measurement's search by title field.
/// </summary>
private string searchTester;
/// <summary>
/// Measurement's search by vehicle VIN field.
/// </summary>
private string searchVehicleVIN;
public MeasurementModel MeasurementModel
{
get;
set;
}
public enum SelectedState
{
// No Masurements.
Inactive,
// Masurements.
Active,
// Waiting for Masurements.
WaitingAnswer
};
public SelectedState CurrentSelectedState { get; set; }
public Analysis Analysis
{
get;
set;
}
public AnalysisViewModel()
{
Analysis = new Analysis();
measurements = new ListCollectionView(MeasurementModel.Measurements);
measurements.Filter = new Predicate<object>(SearchCallbackAnalysis);
}
~AnalysisViewModel()
{
}
/// <summary>
/// List of measurements that will be displayed in analysis view.
/// </summary>
public ICollectionView Measurements
{
get { return measurements; }
set { measurements = value; }
}
/// <summary>
/// Gets or sets new analysis command.
/// </summary>
public ICommand NewAnalysis
{
get
{
if (newAnalysis == null)
newAnalysis = new NewAnalysisCommand(this);
return newAnalysis;
}
}
public bool SearchCallbackAnalysis(object item)
{
bool isItemShowed = true;
if ((searchTitle != "") && (searchTitle != null))
isItemShowed &= (((Measurement)item).Title == searchTitle);
if ((searchVehicleVIN != "") && (searchVehicleVIN != null))
isItemShowed &= (((Measurement)item).Vehicle.VehicleVIN == searchVehicleVIN);
if ((SearchTester != "") && (SearchTester != null))
isItemShowed &= (((Measurement)item).Tester == SearchTester);
return isItemShowed;
}
/// <summary>
/// Gets or sets measurement's search by title field.
/// </summary>
public string SearchTitle
{
get
{
return searchTitle;
}
set
{
searchTitle = value;
Measurements.Refresh();
}
}
/// <summary>
/// Gets or sets measurement's search by tester name field.
/// </summary>
public string SearchTester
{
get
{
return searchTester;
}
set
{
searchTester = value;
Measurements.Refresh();
}
}
/// <summary>
/// Gets or sets measurement's search by vehicle VIN field.
/// </summary>
public string SearchVehicleVIN
{
get
{
return searchVehicleVIN;
}
set
{
searchVehicleVIN = value;
Measurements.Refresh();
}
}
}//end AnalysisViewModel
}//end namespace AnalysisViewModel
if i comment in constructor this line of code:
measurements.Filter = new Predicate<object>(SearchCallbackAnalysis);
Everything works fine but i need this line to search in the list.
Additional info:
xamlReader Cannot obtain value of local or argument 'xamlReader' as it is not available at this instruction pointer, possibly because it has been optimized away. System.Xaml.XamlReader
writerFactory Cannot obtain value of local or argument 'writerFactory' as it is not available at this instruction pointer, possibly because it has been optimized away. System.Xaml.IXamlObjectWriterFactory
skipJournaledProperties Cannot obtain value of local or argument 'skipJournaledProperties' as it is not available at this instruction pointer, possibly because it has been optimized away. bool
rootObject Cannot obtain value of local or argument 'rootObject' as it is not available at this instruction pointer, possibly because it has been optimized away. object
settings Cannot obtain value of local or argument 'settings' as it is not available at this instruction pointer, possibly because it has been optimized away. System.Xaml.XamlObjectWriterSettings
baseUri Cannot obtain value of local or argument 'baseUri' as it is not available at this instruction pointer, possibly because it has been optimized away. System.Uri
i have try:
public ICollectionView Measurements
{
get { return measurements; }
set { measurements = value;
measurements.Filter = new Predicate<object>(SearchCallbackAnalysis);
}
}
Now everything works fine. Thank you for try to help me.
I have recently updated PostSharp to v4.1.13 and I have started receiving this error when I try to build my solution:
The custom attribute 'True.Kentico.Caching.KenticoCacheAttribute' constructor threw the exception EntryPointNotFoundException: Entry point was not found.
This attribute implements caching, and has worked before, so I'm wondering what would be the cause of it breaking. I am using this attribute in other assemblies referencing the aspect's containing assembly, and I get that error in there as well.
The following is the code for the attribute. Apologies, it contains a number of constructors.
[Serializable]
public class KenticoCacheAttribute : MethodInterceptionAspect
{
public int CacheMinutes { get; set; }
/// <summary>
/// the string value of the cache dependency key. If it uses parameters from the method, include a {0} to format with the method parameter
/// </summary>
public string CacheDependency { get; set; }
public string[] CacheDependencyStrings { get; set; }
public KenticoCacheDependencyObtainFrom ObtainCacheDependencyFrom { get; set; }
/// <summary>
/// whether caching is enabled - default from app settings
/// </summary>
public bool CacheEnabled { get; set; }
/// <summary>
/// this is the index of the parameter that will be used to format the cachedependency, if required
/// </summary>
public int CacheDependencyParameterIndex { get; set; }
public string CacheDependencyObjectProperty { get; set; }
private string _methodName;
/// <summary>
/// initializes the cache attribute to use a static dependency
/// </summary>
/// <param name="cacheDependency"></param>
public KenticoCacheAttribute(string cacheDependency)
{
CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting;
CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache;
CacheDependencyParameterIndex = -1;
CacheDependency = cacheDependency;
if (String.IsNullOrEmpty(cacheDependency))
ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.NoDependency;
else
{
ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.Static;
}
}
/// <summary>
/// initializes the cache attribute to use a static dependency using multiple depndencies
/// </summary>
/// <param name="cacheDependencystrings">an array of strings</param>
public KenticoCacheAttribute(string[] cacheDependencystrings)
{
CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting;
CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache;
CacheDependencyParameterIndex = -1;
CacheDependencyStrings = cacheDependencystrings;
ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.MultipleDependenciesStatic;
}
/// <summary>
/// initializes the cache attribute to take the dependency from the input parameter
/// </summary>
/// <param name="cacheDependency">the static string to be used for the cache dependency. you may include {CurrentSiteName} to be replaced with the current site name, and {0} to be replaced with the value passed in as one of the method parameters</param>
/// <param name="cacheDependencyParameterIndex">the index of the parameter in the method parameters that will be used to create the cache dependency key</param>
public KenticoCacheAttribute(string cacheDependency, int cacheDependencyParameterIndex)
{
CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting;
CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache;
CacheDependencyParameterIndex = cacheDependencyParameterIndex;
CacheDependency = cacheDependency;
if (String.IsNullOrEmpty(cacheDependency))
{
ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.MultipleDependenciesFromParameter;
}
else
{
ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.FromMethodParameter;
}
}
/// <summary>
/// initialize the cache attribute to obtain the cache dependency from the named property of the return object
/// </summary>
/// <param name="cacheDependency">the static string to be used for the cache dependency. you may include {CurrentSiteName} to be replaced with the current site name, and {0} to be replaced with the value in the named parameter </param>
/// <param name="cacheDependencyObjectPropertyName">the name of the property of the return object that will be used to replace the placeholder in the static string to build the cache dependency string</param>
public KenticoCacheAttribute(string cacheDependency, string cacheDependencyObjectPropertyName)
{
CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting;
CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache;
CacheDependencyParameterIndex = -1;
CacheDependency = cacheDependency;
ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.FromReturnObject;
CacheDependencyObjectProperty = cacheDependencyObjectPropertyName;
}
/// <summary>
/// initialize the cache attribute to obtain the cache dependency from the named property of the return object
/// </summary>
/// <param name="cacheDependency">the static string to be used for the cache dependency. you may include {CurrentSiteName} to be replaced with the current site name, and {0} to be replaced with the value in the named parameter </param>
/// <param name="cacheDependencyParameterIndex"></param>
/// <param name="cacheDependencyObjectPropertyName">the name of the property of the return object that will be used to replace the placeholder in the static string to build the cache dependency string</param>
public KenticoCacheAttribute(string cacheDependency, int cacheDependencyParameterIndex, string cacheDependencyObjectPropertyName)
{
CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting;
CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache;
CacheDependencyParameterIndex = cacheDependencyParameterIndex;
CacheDependency = cacheDependency;
ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.FromMethodParameterObjectProperty;
CacheDependencyObjectProperty = cacheDependencyObjectPropertyName;
}
public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
{
_methodName = method.Name;
}
public override void OnInvoke(MethodInterceptionArgs args)
{
var methodInfo = args.Method as MethodInfo;
if (methodInfo != null && (methodInfo.ReturnType != typeof(void) && CacheEnabled))
{
var cacheKey = BuildCacheKey(args.Arguments);
var cacheSettings = new CacheSettings(CacheMinutes, cacheKey);
var data = CacheHelper.Cache(cs => GetData(cs, args), cacheSettings);
args.ReturnValue = data;
}
else
base.OnInvoke(args);
}
private object GetData(CacheSettings cs, MethodInterceptionArgs args)
{
var data = args.Invoke(args.Arguments);
// Checks whether data was loaded and whether the data should be cached (based on the CacheSettings)
if ((data != null) && cs.Cached)
{
// Sets a cache dependency for the data
// The data is removed from the cache if the objects represented by the dummy key are modified (all user objects in this case)
var dependencyResolver = CacheDependencyFactory.GetDependecyFormatter(ObtainCacheDependencyFrom);
var dependencyString = dependencyResolver.Format(new CacheDependencyFormatParameters()
{
CacheDependencyBase = CacheDependency,
CacheDependencybaseString = CacheDependencyStrings,
InputParameterIndex = CacheDependencyParameterIndex,
ReturnParameterName = CacheDependencyObjectProperty,
InputParameterData = args,
ReturnParameterData = data
});
cs.CacheDependency = CacheHelper.GetCacheDependency(dependencyString);
}
return data;
}
private string BuildCacheKey(Arguments arguments)
{
var sb = new StringBuilder();
sb.Append(_methodName);
foreach (var argument in arguments.ToArray())
{
sb.Append(argument == null ? "_" : argument.ToString());
}
sb.Append(String.Format("{0}_{1}", SiteContext.CurrentSiteName,
SiteContext.CurrentSite.DefaultVisitorCulture));
return sb.ToString();
}
}
Any help would be appreciated!
So it turns out that the issue is with these lines of code in the constructors:
CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting;
CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache;
When PostSharp runs, The dependencyInjection framework has not yet been initialised, so the execution fails. It's strange that it was working before, but hey. I moved this code into the OnInvoke method, which should only run when the DI Enginecontext has been initialised :
public override void OnInvoke(MethodInterceptionArgs args)
{
CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting;
CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache;
var methodInfo = args.Method as MethodInfo;
......
I have been reading on how to compare a list with one annother. I have tried to implement the IEquatable interface. Here is what i have done so far:
/// <summary>
/// A object holder that contains a service and its current failcount
/// </summary>
public class ServiceHolder : IEquatable<ServiceHolder>
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="service"></param>
public ServiceHolder(Service service)
{
Service = service;
CurrentFailCount = 0;
}
public Service Service { get; set; }
public UInt16 CurrentFailCount { get; set; }
/// <summary>
/// Public equal method
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
ServiceHolder tmp = obj as ServiceHolder;
if (tmp == null)
{
return false;
}
else
{
return Equals(tmp);
}
}
/// <summary>
/// Checks the internal components compared to one annother
/// </summary>
/// <param name="serviceHolder"></param>
/// <returns>tru eif they are the same else false</returns>
public bool Equals(ServiceHolder serviceHolder)
{
if (serviceHolder == null)
{
return false;
}
if (this.Service.Id == serviceHolder.Service.Id)
{
if (this.Service.IpAddress == serviceHolder.Service.IpAddress)
{
if (this.Service.Port == serviceHolder.Service.Port)
{
if (this.Service.PollInterval == serviceHolder.Service.PollInterval)
{
if (this.Service.ServiceType == serviceHolder.Service.ServiceType)
{
if (this.Service.Location == serviceHolder.Service.Location)
{
if (this.Service.Name == this.Service.Name)
{
return true;
}
}
}
}
}
}
}
return false;
}
}
and this is where I use it:
private void CheckIfServicesHaveChangedEvent()
{
IList<ServiceHolder> tmp;
using (var db = new EFServiceRepository())
{
tmp = GetServiceHolders(db.GetAll());
}
if (tmp.Equals(Services))
{
StateChanged = true;
}
else
{
StateChanged = false;
}
}
Now when I debug and I put a break point in the equals function it never gets hit.
This leads me to think I have implemented it incorrectly or Im not calling it correctly?
If you want to compare the contents of two lists then the best method is SequenceEqual.
if (tmp.SequenceEquals(Services))
This will compare the contents of both lists using equality semantics on the values in the list. In this case the element type is ServiceHolder and as you've already defined equality semantics for this type it should work just fine
EDIT
OP commented that order of the collections shouldn't matter. For that scenario you can do the following
if (!tmp.Except(Services).Any())
You can compare lists without the order most easily with linq.
List<ServiceHolder> result = tmp.Except(Services).ToList();
This question already has answers here:
Retrieve AssemblyCompanyName from Class Library
(6 answers)
Closed 9 years ago.
Is it possible to retrieve this value at runtime?
I'd like to keep the value in one place, and retrieve it whenever my application needs to output the name of my company.
This should do it:
using System.Reflection;
string company = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(
Assembly.GetExecutingAssembly(), typeof(AssemblyCompanyAttribute), false))
.Company;
If you really want to keep the value in one place and you have multiple assemblies in your solution, you could either:
Use GetEntryAssembly instead of GetExecutingAssembly and set the company attribute only on your entry assembly, or better:
Use a central assembly info file, see this answer
UPDATE
Improved the code by suggestion of #hmemcpy so it doesn't need [0] anymore. Thanks!
If you want a nice generic method for fetching the assembly Info data try the following:
public string GetAssemblyAttribute<T>(Func<T, string> value)
where T : Attribute
{
T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof (T));
return value.Invoke(attribute);
}
So you can then call
string title = GetAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title);
string copyright = GetAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright);
string version = GetAssemblyAttribute<AssemblyVersionAttribute>(a => a.Version);
string description = GetAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description);
Building on Adam Price's very elegant method -- with the correct comments from Mark about it not working for Version... Came up with the following methods -- one for getting the current executing AssemblyInfo properties -- and one for getting the "calling" assembly's AssemblyInfo properties.
Have it up on my blog at http://lancelarsen.com/reading-values-from-assemblyinfo-cs/
/// <summary>
/// Gets the values from the AssemblyInfo.cs file for the current executing assembly
/// </summary>
/// <example>
/// string company = AssemblyInfo.Company;
/// string product = AssemblyInfo.Product;
/// string copyright = AssemblyInfo.Copyright;
/// string trademark = AssemblyInfo.Trademark;
/// string title = AssemblyInfo.Title;
/// string description = AssemblyInfo.Description;
/// string configuration = AssemblyInfo.Configuration;
/// string fileversion = AssemblyInfo.FileVersion;
/// Version version = AssemblyInfo.Version;
/// string versionFull = AssemblyInfo.VersionFull;
/// string versionMajor = AssemblyInfo.VersionMajor;
/// string versionMinor = AssemblyInfo.VersionMinor;
/// string versionBuild = AssemblyInfo.VersionBuild;
/// string versionRevision = AssemblyInfo.VersionRevision;
/// </example>
static public class AssemblyInfo
{
public static string Company { get { return GetExecutingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
public static string Product { get { return GetExecutingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
public static string Copyright { get { return GetExecutingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
public static string Trademark { get { return GetExecutingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
public static string Title { get { return GetExecutingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
public static string Description { get { return GetExecutingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
public static string Configuration { get { return GetExecutingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
public static string FileVersion { get { return GetExecutingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }
public static Version Version { get { return Assembly.GetExecutingAssembly().GetName().Version; } }
public static string VersionFull { get { return Version.ToString(); } }
public static string VersionMajor { get { return Version.Major.ToString(); } }
public static string VersionMinor { get { return Version.Minor.ToString(); } }
public static string VersionBuild { get { return Version.Build.ToString(); } }
public static string VersionRevision { get { return Version.Revision.ToString(); } }
private static string GetExecutingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
{
T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(T));
return value.Invoke(attribute);
}
}
/// <summary>
/// Gets the values from the AssemblyInfo.cs file for the previous assembly
/// </summary>
/// <example>
/// AssemblyInfoCalling assembly = new AssemblyInfoCalling();
/// string company1 = assembly.Company;
/// string product1 = assembly.Product;
/// string copyright1 = assembly.Copyright;
/// string trademark1 = assembly.Trademark;
/// string title1 = assembly.Title;
/// string description1 = assembly.Description;
/// string configuration1 = assembly.Configuration;
/// string fileversion1 = assembly.FileVersion;
/// Version version1 = assembly.Version;
/// string versionFull1 = assembly.VersionFull;
/// string versionMajor1 = assembly.VersionMajor;
/// string versionMinor1 = assembly.VersionMinor;
/// string versionBuild1 = assembly.VersionBuild;
/// string versionRevision1 = assembly.VersionRevision;
/// </example>
public class AssemblyInfoCalling
{
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyInfoCalling"/> class.
/// </summary>
/// <param name="traceLevel">The trace level needed to get correct assembly
/// - will need to adjust based on where you put these classes in your project(s).</param>
public AssemblyInfoCalling(int traceLevel = 4)
{
//----------------------------------------------------------------------
// Default to "3" as the number of levels back in the stack trace to get the
// correct assembly for "calling" assembly
//----------------------------------------------------------------------
StackTraceLevel = traceLevel;
}
//----------------------------------------------------------------------
// Standard assembly attributes
//----------------------------------------------------------------------
public string Company { get { return GetCallingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
public string Product { get { return GetCallingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
public string Copyright { get { return GetCallingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
public string Trademark { get { return GetCallingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
public string Title { get { return GetCallingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
public string Description { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
public string Configuration { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
public string FileVersion { get { return GetCallingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }
//----------------------------------------------------------------------
// Version attributes
//----------------------------------------------------------------------
public static Version Version
{
get
{
//----------------------------------------------------------------------
// Get the assembly, return empty if null
//----------------------------------------------------------------------
Assembly assembly = GetAssembly(StackTraceLevel);
return assembly == null ? new Version() : assembly.GetName().Version;
}
}
public string VersionFull { get { return Version.ToString(); } }
public string VersionMajor { get { return Version.Major.ToString(); } }
public string VersionMinor { get { return Version.Minor.ToString(); } }
public string VersionBuild { get { return Version.Build.ToString(); } }
public string VersionRevision { get { return Version.Revision.ToString(); } }
//----------------------------------------------------------------------
// Set how deep in the stack trace we're looking - allows for customized changes
//----------------------------------------------------------------------
public static int StackTraceLevel { get; set; }
/// <summary>
/// Gets the calling assembly attribute.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <example>return GetCallingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company);</example>
/// <returns></returns>
private string GetCallingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
{
//----------------------------------------------------------------------
// Get the assembly, return empty if null
//----------------------------------------------------------------------
Assembly assembly = GetAssembly(StackTraceLevel);
if (assembly == null) return string.Empty;
//----------------------------------------------------------------------
// Get the attribute value
//----------------------------------------------------------------------
T attribute = (T) Attribute.GetCustomAttribute(assembly, typeof (T));
return value.Invoke(attribute);
}
/// <summary>
/// Go through the stack and gets the assembly
/// </summary>
/// <param name="stackTraceLevel">The stack trace level.</param>
/// <returns></returns>
private static Assembly GetAssembly(int stackTraceLevel)
{
//----------------------------------------------------------------------
// Get the stack frame, returning null if none
//----------------------------------------------------------------------
StackTrace stackTrace = new StackTrace();
StackFrame[] stackFrames = stackTrace.GetFrames();
if (stackFrames == null) return null;
//----------------------------------------------------------------------
// Get the declaring type from the associated stack frame, returning null if nonw
//----------------------------------------------------------------------
var declaringType = stackFrames[stackTraceLevel].GetMethod().DeclaringType;
if (declaringType == null) return null;
//----------------------------------------------------------------------
// Return the assembly
//----------------------------------------------------------------------
var assembly = declaringType.Assembly;
return assembly;
}
}
I have a desktop application written in C# I'd like to make scriptable on C#/VB.
Ideally, the user would open a side pane and write things like
foreach (var item in myApplication.Items)
item.DoSomething();
Having syntax highlighting and code completion would be awesome, but I could live without it.
I would not want to require users to have Visual Studio 2010 installed.
I am thinking about invoking the compiler, loading and running the output assembly.
Is there a better way?
Is Microsoft.CSharp the answer?
Have you thought about IronPython or IronRuby?
Use a scripting language. Tcl, LUA or even JavaScript comes to mind.
Using Tcl is really easy:
using System.Runtime.InteropServices;
using System;
namespace TclWrap {
public class TclAPI {
[DllImport("tcl84.DLL")]
public static extern IntPtr Tcl_CreateInterp();
[DllImport("tcl84.Dll")]
public static extern int Tcl_Eval(IntPtr interp,string skript);
[DllImport("tcl84.Dll")]
public static extern IntPtr Tcl_GetObjResult(IntPtr interp);
[DllImport("tcl84.Dll")]
public static extern string Tcl_GetStringFromObj(IntPtr tclObj,IntPtr length);
}
public class TclInterpreter {
private IntPtr interp;
public TclInterpreter() {
interp = TclAPI.Tcl_CreateInterp();
if (interp == IntPtr.Zero) {
throw new SystemException("can not initialize Tcl interpreter");
}
}
public int evalScript(string script) {
return TclAPI.Tcl_Eval(interp,script);
}
public string Result {
get {
IntPtr obj = TclAPI.Tcl_GetObjResult(interp);
if (obj == IntPtr.Zero) {
return "";
} else {
return TclAPI.Tcl_GetStringFromObj(obj,IntPtr.Zero);
}
}
}
}
}
Then use it like:
TclInterpreter interp = new TclInterpreter();
string result;
if (interp.evalScript("set a 3; {exp $a + 2}")) {
result = interp.Result;
}
I would use PowerShell or MEF. It really depends on what you mean by scritable and what type of application you have. The best part about PowerShell is it's directly hostable and directly designed to use .NET interfaces in a scripting manner.
I had the exact same problem and with a bit of googling and few modifications I solved it using Microsoft.CSharp.CSharpCodeProvider which allows the user to edit a C# template I present to them that exposes the complete Object Model of my application and they can even pass parameters from / and return result to the application itself.
The full C# solution can be downloaded from http://qurancode.com.
But here is the main code that does just that:
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Security;
using Model; // this is my application Model with my own classes
public static class ScriptRunner
{
private static string s_scripts_directory = "Scripts";
static ScriptRunner()
{
if (!Directory.Exists(s_scripts_directory))
{
Directory.CreateDirectory(s_scripts_directory);
}
}
/// <summary>
/// Load a C# script fie
/// </summary>
/// <param name="filename">file to load</param>
/// <returns>file content</returns>
public static string LoadScript(string filename)
{
StringBuilder str = new StringBuilder();
string path = s_scripts_directory + "/" + filename;
if (File.Exists(filename))
{
using (StreamReader reader = File.OpenText(path))
{
string line = "";
while ((line = reader.ReadLine()) != null)
{
str.AppendLine(line);
}
}
}
return str.ToString();
}
/// <summary>
/// Compiles the source_code
/// </summary>
/// <param name="source_code">source_code must implements IScript interface</param>
/// <returns>compiled Assembly</returns>
public static CompilerResults CompileCode(string source_code)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = false; // generate a Class Library assembly
options.GenerateInMemory = true; // so we don;t have to delete it from disk
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
options.ReferencedAssemblies.Add(assembly.Location);
}
return provider.CompileAssemblyFromSource(options, source_code);
}
/// <summary>
/// Execute the IScriptRunner.Run method in the compiled_assembly
/// </summary>
/// <param name="compiled_assembly">compiled assembly</param>
/// <param name="args">method arguments</param>
/// <returns>object returned</returns>
public static object Run(Assembly compiled_assembly, object[] args, PermissionSet permission_set)
{
if (compiled_assembly != null)
{
// security is not implemented yet !NIY
// using Utilties.PrivateStorage was can save but not diaplay in Notepad
// plus the output is saved in C:\Users\<user>\AppData\Local\IsolatedStorage\...
// no contral over where to save make QuranCode unportable applicaton, which is a no no
//// restrict code security
//permission_set.PermitOnly();
foreach (Type type in compiled_assembly.GetExportedTypes())
{
foreach (Type interface_type in type.GetInterfaces())
{
if (interface_type == typeof(IScriptRunner))
{
ConstructorInfo constructor = type.GetConstructor(System.Type.EmptyTypes);
if ((constructor != null) && (constructor.IsPublic))
{
// construct object using default constructor
IScriptRunner obj = constructor.Invoke(null) as IScriptRunner;
if (obj != null)
{
return obj.Run(args);
}
else
{
throw new Exception("Invalid C# code!");
}
}
else
{
throw new Exception("No default constructor was found!");
}
}
else
{
throw new Exception("IScriptRunner is not implemented!");
}
}
}
// revert security restrictions
//CodeAccessPermission.RevertPermitOnly();
}
return null;
}
/// <summary>
/// Execute a public static method_name(args) in compiled_assembly
/// </summary>
/// <param name="compiled_assembly">compiled assembly</param>
/// <param name="methode_name">method to execute</param>
/// <param name="args">method arguments</param>
/// <returns>method execution result</returns>
public static object ExecuteStaticMethod(Assembly compiled_assembly, string methode_name, object[] args)
{
if (compiled_assembly != null)
{
foreach (Type type in compiled_assembly.GetTypes())
{
foreach (MethodInfo method in type.GetMethods())
{
if (method.Name == methode_name)
{
if ((method != null) && (method.IsPublic) && (method.IsStatic))
{
return method.Invoke(null, args);
}
else
{
throw new Exception("Cannot invoke method :" + methode_name);
}
}
}
}
}
return null;
}
/// <summary>
/// Execute a public method_name(args) in compiled_assembly
/// </summary>
/// <param name="compiled_assembly">compiled assembly</param>
/// <param name="methode_name">method to execute</param>
/// <param name="args">method arguments</param>
/// <returns>method execution result</returns>
public static object ExecuteInstanceMethod(Assembly compiled_assembly, string methode_name, object[] args)
{
if (compiled_assembly != null)
{
foreach (Type type in compiled_assembly.GetTypes())
{
foreach (MethodInfo method in type.GetMethods())
{
if (method.Name == methode_name)
{
if ((method != null) && (method.IsPublic))
{
object obj = Activator.CreateInstance(type, null);
return method.Invoke(obj, args);
}
else
{
throw new Exception("Cannot invoke method :" + methode_name);
}
}
}
}
}
return null;
}
}
I then defined a C# Interface to be implemented by the user code where they are free to put anythng they like inside their concrete Run method:
/// <summary>
/// Generic method runner takes any number and type of args and return any type
/// </summary>
public interface IScriptRunner
{
object Run(object[] args);
}
And here is the startup template the user can extends:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;
using Model;
public class MyScript : IScriptRunner
{
private string m_scripts_directory = "Scripts";
/// <summary>
/// Run implements IScriptRunner interface
/// to be invoked by QuranCode application
/// with Client, current Selection.Verses, and extra data
/// </summary>
/// <param name="args">any number and type of arguments</param>
/// <returns>return any type</returns>
public object Run(object[] args)
{
try
{
if (args.Length == 3) // ScriptMethod(Client, List<Verse>, string)
{
Client client = args[0] as Client;
List<Verse> verses = args[1] as List<Verse>;
string extra = args[2].ToString();
if ((client != null) && (verses != null))
{
return MyMethod(client, verses, extra);
}
}
return null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName);
return null;
}
}
/// <summary>
/// Write your C# script insde this method.
/// Don't change its name or parameters
/// </summary>
/// <param name="client">Client object holding a reference to the currently selected Book object in TextMode (eg Simplified29)</param>
/// <param name="verses">Verses of the currently selected Chapter/Page/Station/Part/Group/Quarter/Bowing part of the Book</param>
/// <param name="extra">any user parameter in the TextBox next to the EXE button (ex Frequency, LettersToJump, DigitSum target, etc)</param>
/// <returns>true to disply back in QuranCode matching verses. false to keep script window open</returns>
private long MyMethod(Client client, List<Verse> verses, string extra)
{
if (client == null) return false;
if (verses == null) return false;
if (verses.Count == 0) return false;
int target;
if (extra == "")
{
target = 0;
}
else
{
if (!int.TryParse(extra, out target))
{
return false;
}
}
try
{
long total_value = 0L;
foreach (Verse verse in verses)
{
total_value += Client.CalculateValue(verse.Text);
}
return total_value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName);
return 0L;
}
}
}
And this is how I call it from my MainForm.cs
#region Usage from MainForm
if (!ScriptTextBox.Visible)
{
ScriptTextBox.Text = ScriptRunner.LoadScript(#"Scripts\Template.cs");
ScriptTextBox.Visible = true;
}
else // if visible
{
string source_code = ScriptTextBox.Text;
if (source_code.Length > 0)
{
Assembly compiled_assembly = ScriptRunner.CompileCode(source_code);
if (compiled_assembly != null)
{
object[] args = new object[] { m_client, m_client.Selection.Verses, "19" };
object result = ScriptRunner.Run(compiled_assembly, args);
// process result here
}
}
ScriptTextBox.Visible = false;
}
#endregion
Still to do is the Syntax Highlighting and CodeCompletion though.
Good luck!
You will invoke the compiler anyway, because C# is a compiled language. The best way to do it can be checked in CSharpCodeProvider - класс.
You can use the following open source solution as an example: https://bitbucket.org/jlyonsmith/coderunner/wiki/Home
What language is your application written in? If C++, you might consider Google V8, an embeddable ECMAScript/JavaScript engine.