Created
October 28, 2013 17:32
-
-
Save tekiegirl/7201063 to your computer and use it in GitHub Desktop.
Get the executing assembly of a web app referencing a class library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Type[] types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Type[] types = System.Reflection.Assembly.GetEntryAssembly().GetTypes(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
http://jacstech.wordpress.com/2013/09/05/get-the-executing-assembly-of-a-web-app-referencing-a-class-library/