Skip to content

Instantly share code, notes, and snippets.

@tekiegirl
Created October 28, 2013 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tekiegirl/7201063 to your computer and use it in GitHub Desktop.
Save tekiegirl/7201063 to your computer and use it in GitHub Desktop.
Get the executing assembly of a web app referencing a class library
private static bool CheckValid(string controller, string action)
{
if (!controller.Contains("Controller"))
{
controller = controller + "Controller";
}
// Get assembly
Assembly a = GetWebEntryAssembly();
if (a != null)
{
// Get types
Type[] types = a.GetTypes();
// Get controller with this name, if it exists
Type type = types.Where( t => t.Name == controller ).SingleOrDefault();
// Check if controller contains action, if specified
if (string.IsNullOrEmpty(action))
{
// no action specified
return type != null ? true : false;
}
return (type != null && type.GetMethod(action) != null) ? true : false;
}
return false;
}
private static Assembly GetWebEntryAssembly()
{
if (System.Web.HttpContext.Current == null || System.Web.HttpContext.Current.ApplicationInstance == null)
{
return null;
}
var type = System.Web.HttpContext.Current.ApplicationInstance.GetType();
while (type != null && type.Namespace == "ASP")
{
type = type.BaseType;
}
return type == null ? null : type.Assembly;
}
Type[] types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
Type[] types = System.Reflection.Assembly.GetEntryAssembly().GetTypes();
Assembly a = new StackTrace().GetFrames().Last().GetMethod().Module.Assembly;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment