本文整理汇总了C#中Controller.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Controller.GetType方法的具体用法?C# Controller.GetType怎么用?C# Controller.GetType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Controller
的用法示例。
在下文中一共展示了Controller.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AssertFieldsAreNotNull
public static void AssertFieldsAreNotNull( Controller controller )
{
var allReadOnlyFields = new List<FieldInfo>();
Type controllerType = controller.GetType();
while ( controllerType != null )
{
var fields =
controllerType
.GetFields( BindingFlags.NonPublic | BindingFlags.Instance )
.Where( x => x.IsInitOnly );
allReadOnlyFields.AddRange( fields );
controllerType = controllerType.BaseType;
}
var fieldsWithNullValue = allReadOnlyFields
.Where( x => x.GetValue( controller ) == null )
.ToList();
if ( fieldsWithNullValue.Count > 0 )
{
Console.WriteLine( "The Following " + fieldsWithNullValue.Count + " fields are not set" );
fieldsWithNullValue.ForEach( x => Console.WriteLine( x.Name ) );
Assert.Fail();
}
}
示例2: InvokeAction
public IActionResult InvokeAction(Controller controller, ActionDescriptor actionDescriptor)
{
/*
* Child processes that use such C run-time functions as printf() and fprintf() can behave poorly when redirected.
* The C run-time functions maintain separate IO buffers. When redirected, these buffers might not be flushed immediately after each IO call.
* As a result, the output to the redirection pipe of a printf() call or the input from a getch() call is not flushed immediately and delays, sometimes-infinite delays occur.
* This problem is avoided if the child process flushes the IO buffers after each call to a C run-time IO function.
* Only the child process can flush its C run-time IO buffers. A process can flush its C run-time IO buffers by calling the fflush() function.
*/
var methodWithIntParameter = controller.GetType()
.GetMethods()
.FirstOrDefault(x => x.Name.ToLower() == actionDescriptor.ActionName.ToLower() &&
x.GetParameters().Length == 1 &&
x.GetParameters()[0].ParameterType == typeof(string) &&
x.ReturnType == typeof(IActionResult));
if (methodWithIntParameter == null)
{
throw new HttpNotFoundException(
string.Format(
"Expected method with signature IActionResult {0}(string) in class {1}Controller",
actionDescriptor.ActionName,
actionDescriptor.ControllerName));
}
try
{
var actionResult = (IActionResult)
methodWithIntParameter.Invoke(controller, new object[] { actionDescriptor.Parameter });
return actionResult;
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
}
示例3: InvokeAction
public IActionResult InvokeAction(Controller controller, ActionDescriptor actionDescriptor)
{
var methodWithIntParameter =
controller.GetType()
.GetMethods()
.FirstOrDefault(
x =>
x.Name.ToLower() == actionDescriptor.ActionName.ToLower() && x.GetParameters().Length == 1
&& x.GetParameters()[0].ParameterType == typeof(string)
&& x.ReturnType == typeof(IActionResult));
if (methodWithIntParameter == null)
{
throw new HttpNotFoundException(
string.Format(
"Expected method with signature IActionResult {0}(string) in class {1}",
actionDescriptor.ActionName,
actionDescriptor.ControllerName));
}
try
{
var actionResult =
(IActionResult)methodWithIntParameter.Invoke(controller, new object[] { actionDescriptor.Parameter });
return actionResult;
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
}
示例4: IsHavePower
private bool IsHavePower(IList<EmpModFunc> empModFuncs, Controller serviceBase, FuncEnum funcName)
{
bool retVal = false;
string cls = serviceBase.GetType().FullName;
if (empModFuncs != null)
{
foreach (EmpModFunc empModFunc in empModFuncs)
{
//查找用户是否存在类的权限
if (empModFunc.ModFunc.Cls.Equals(cls))
{
if (empModFunc.FuncNames == null)
{
empModFunc.FuncNames = "";
}
//把函数枚举转为字符串形式,如"|Add|"
string funcNameStr = "|" + funcName + "|";
//查找函数权限
int find = empModFunc.FuncNames.IndexOf(funcNameStr);
if (find > -1)
{
retVal = true;
break;
}
}
}
}
return retVal;
}
示例5: AssertAttribute
private static void AssertAttribute(Controller controller, string actionMethodName, Type attribute, Type[] parameterTypes)
{
var type = controller.GetType();
var methodInfo = type.GetMethod(actionMethodName, parameterTypes ?? new Type[0]);
var attributes = methodInfo.GetCustomAttributes(attribute, true);
Assert.IsTrue(attributes.Any(), string.Format("{0} not found on action {1}", attribute.Name, actionMethodName));
}
示例6: CustomActionParamsMapper
/// <summary>
/// Initializes a new instance of the <see cref="CustomActionParamsMapper"/> class.
/// </summary>
/// <param name="controller">The controller.</param>
/// <param name="routeTemplateResolver">This function should return the route template that the mapper will use.</param>
/// <param name="actionName">Name of the action.</param>
/// <exception cref="System.ArgumentNullException">routeTemplateResolver</exception>
public CustomActionParamsMapper(Controller controller, Func<string> routeTemplateResolver, string actionName = CustomActionParamsMapper.DefaultActionName)
: base(controller)
{
if (routeTemplateResolver == null)
throw new ArgumentNullException("routeTemplateResolver");
this.actionName = actionName;
this.actionMethod = controller.GetType().GetMethod(this.actionName, BindingFlags.Instance | BindingFlags.Public);
this.routeTemplateResolver = routeTemplateResolver;
}
示例7: CreateControllerContext
private static ControllerContext CreateControllerContext(Controller controller)
{
if (HttpContext.Current == null) throw new InvalidOperationException("HttpContext unavailable");
var routeData = new RouteData();
if (routeData.Values.ContainsKey("controller") == false && routeData.Values.ContainsKey("Controller") == false)
{
routeData.Values.Add("controller", controller.GetType().Name.ToLower().Replace("controller", ""));
}
return new ControllerContext(new HttpContextWrapper(HttpContext.Current), routeData, controller);
}
示例8: GetAttributesFor
public static IEnumerable<Type> GetAttributesFor(Controller controller, String methodName)
{
var type = controller.GetType();
var methodInfo = type.GetMethod(methodName);
if (methodInfo == null)
return Enumerable.Empty<Type>();
var methodAttributes = methodInfo.GetCustomAttributes(false);
return methodAttributes.Select(a => a.GetType());
}
示例9: Log
internal void Log(string userName, string action, Controller sender)
{
Log log = new Log()
{
UserName = userName,
Action = action,
Time = DateTime.Now,
Controller = sender.GetType().Namespace,
};
_logRepo.Create(log);
}
示例10: ProtectsFromOverposting
protected void ProtectsFromOverposting(Controller controller, String postMethod, String properties)
{
MethodInfo methodInfo = controller
.GetType()
.GetMethods()
.First(method =>
method.Name == postMethod &&
method.IsDefined(typeof(HttpPostAttribute), false));
BindAttribute actual = methodInfo
.GetParameters()
.First()
.GetCustomAttribute<BindAttribute>(false);
Assert.Equal(properties, actual.Exclude);
}
示例11: ProtectsFromOverposting
protected void ProtectsFromOverposting(Controller controller, String postMethod, String value)
{
MethodInfo methodInfo = controller
.GetType()
.GetMethods()
.First(method =>
method.Name == postMethod &&
method.IsDefined(typeof(HttpPostAttribute), false));
CustomAttributeData actual = methodInfo
.GetParameters()
.First()
.CustomAttributes
.Single(attr => attr.AttributeType == typeof(BindAttribute));
Assert.Equal(value, actual.NamedArguments.Single().TypedValue.Value);
Assert.Equal("Exclude", actual.NamedArguments.Single().MemberName);
}
示例12: GetControllerPathTransformations
private static IList<Func<string, string>> GetControllerPathTransformations(Controller controller, string customPath)
{
var packagesManager = new PackagesManager();
var currentPackage = packagesManager.GetCurrentPackage();
var pathTransformations = new List<Func<string, string>>();
if (controller.RouteData != null && controller.RouteData.Values.ContainsKey("widgetName"))
{
var widgetName = (string)controller.RouteData.Values["widgetName"];
var controllerType = FrontendManager.ControllerFactory.ResolveControllerType(widgetName);
var widgetVp = FrontendControllerFactory.AppendDefaultPath(FrontendManager.VirtualPathBuilder.GetVirtualPath(controllerType));
pathTransformations.Add(FrontendControllerFactory.GetPathTransformation(widgetVp, currentPackage, widgetName));
}
var controllerVp = customPath ?? FrontendControllerFactory.AppendDefaultPath(FrontendManager.VirtualPathBuilder.GetVirtualPath(controller.GetType().Assembly));
pathTransformations.Add(FrontendControllerFactory.GetPathTransformation(controllerVp, currentPackage));
return pathTransformations;
}
示例13: ItemNotFound
public static ViewResultBase ItemNotFound(Controller trigger, String message)
{
ObjectServices.Logger.CreateLogger(trigger.GetType()).Warn("Item Not Found! Message = {0}", message);
ErrorViewModel viewmodel = new ErrorViewModel();
viewmodel.ErrorText = message;
viewmodel.ErrorRequestPath = trigger.HttpContext.Request.Url.AbsoluteUri;
if (trigger.Request.UrlReferrer != null)
{
viewmodel.ReturnUrl = trigger.Request.UrlReferrer.AbsoluteUri;
}
ViewDataDictionary viewData = new ViewDataDictionary
{
Model = viewmodel
};
ViewResultBase viewresult = trigger.Request.IsAjaxRequest() ? (ViewResultBase)new PartialViewResult() : (ViewResultBase)new ViewResult();
viewresult.ViewName = "ItemNotFound";
viewresult.ViewData = viewData;
return viewresult;
}
示例14: HandleUnknownAction
private static void HandleUnknownAction(Controller controller, string actionName)
{
throw new HttpException(0x194, string.Format(SR.GetString("Controller_UnknownAction"), new object[] { actionName, controller.GetType().FullName }));
}
示例15: ActionIsSecuredByPermission
private bool ActionIsSecuredByPermission(Controller controller, string actionName, string permissionId)
{
var methodInfo = controller.GetType().GetMethod(actionName);
if (methodInfo == null)
throw new InvalidOperationException("Action named '" + actionName + "' not found.");
var attributes = methodInfo.GetCustomAttributes(typeof(UmbracoAuthorizeAttribute), true);
if (attributes.Length == 0)
return false;
return attributes.Cast<UmbracoAuthorizeAttribute>().Any(x => x.Permissions.Length > 0 && x.Permissions.Contains(permissionId));
}