Reputation: 11
Error: ArgumentException: Cannot bind to the target method because its signature is not compatible with that of the delegate type
This is in .NET Core 6; I wanted to add custom html helper textbox which was working in ASP.NET MVC, but it's not working when used in ASP.NET Core.
EditorExtension
method is posted here where delegate error is occurring. and also content some existing helper templates as well. I'm trying to convert this ASP.NET MVC code to ASP.NET Core MVC.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Html;
using RateChangingSystemCore1.MetadataProviders;
using RateChangingSystemCore1.Models;
using RateChangingSystemCore1;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Collections;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.Logging;
namespace RateChangingSystemCore1.Extensions
{
public static class EditorExtensions
{
/// <summary>
/// Access to the log4Net logging object
/// </summary>
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static MethodInfo _inputHelper;
private static Func<IHtmlHelper, InputType, ModelMetadata, string, object, bool, bool, bool, bool, string, IDictionary<string, object>, HtmlString> _inputHelper2;
private static MethodInfo _checkBoxHelper;
private static MethodInfo _textAreaHelper;
static EditorExtensions()
{
_inputHelper = typeof(System.Web.Mvc.Html.InputExtensions).GetMethod("InputHelper", BindingFlags.Static | BindingFlags.NonPublic);
// private static MvcHtmlString InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, string name, object value, bool useViewData, bool isChecked, bool setId, bool isExplicitValue, string format, IDictionary<string, object> htmlAttributes)
// Creates a delegate so that reflection doesn't have to be used to call each time and this method it much faster.
// Verbose due to the number of parameters being passed though but it's just InputExtensions.InputHelper from MVC
// http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/InputExtensions.cs
Delegate @delegate = Delegate.CreateDelegate(typeof(Func<IHtmlHelper, InputType, ModelMetadata, string, object, bool, bool, bool, bool, string, IDictionary<string, object>, HtmlString>), _inputHelper);
_inputHelper2 = (Func<IHtmlHelper, InputType, ModelMetadata, string, object, bool, bool, bool, bool, string, IDictionary<string, object>, HtmlString>)@delegate;
_checkBoxHelper = typeof(System.Web.Mvc.Html.InputExtensions).GetMethod("CheckBoxHelper", BindingFlags.Static | BindingFlags.NonPublic);
_textAreaHelper = typeof(System.Web.Mvc.Html.TextAreaExtensions).GetMethod("TextAreaHelper", BindingFlags.Static | BindingFlags.NonPublic);
}
private static CoopMetadataProvider metadataProvider = new CoopMetadataProvider();
private static ModelMetadata GetDefinitionMetaData(Definition definition)
{
if (HttpContextHelper.Current.Items["definitionMetaDataCache"] is Dictionary<int, ModelMetadata> definitionMetaDataCache)
{
}
else
{
definitionMetaDataCache = new Dictionary<int, ModelMetadata>();
HttpContextHelper.Current.Items["definitionMetaDataCache"] = definitionMetaDataCache;
}
if (definitionMetaDataCache.ContainsKey(definition.NodeID))
{
return (ModelMetadata)definitionMetaDataCache[definition.NodeID];
}
log.DebugFormat("Making meta data for {0}", definition.NodeID);
// Get the model meta data using our custom provider (have to do this this way as CreatMetaData is a protected method
var modelMetadata = metadataProvider.CreateMetadata2(null, typeof(Definition), () => definition, typeof(Definition), null);
definitionMetaDataCache[definition.NodeID] = modelMetadata;
return modelMetadata;
}
private static HtmlString CoopEditorCommon(this IHtmlHelper html, string name, object value, IDictionary<string, object> htmlAttributes, Definition definition, Func<ModelMetadata, object, HtmlString> inputHelper)
{
// ViewData, ModelMetadata, Model, TemplateInfo contain all the information relating to the property and what should be rendered
//var viewData = new IDictionary(htmlAttributes.Keys, htmlAttributes.Values);
\#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
definition = definition ?? html.ViewData\["Definition"\] as Definition;
\#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
// Get the model meta data using our custom provider (have to do this this way as CreateMetaData is a protected method
var modelMetadata = GetDefinitionMetaData(definition);
object formattedModelValue = definition.ValueValue; // modelMetadata.Model;
\#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
string formatString = modelMetadata.EditFormatString;
\#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
if (modelMetadata.ModelType != null && !string.IsNullOrEmpty(formatString))
{
formattedModelValue = string.Format(CultureInfo.CurrentCulture, formatString, definition.ValueValue); // modelMetadata.Model
}
return inputHelper(modelMetadata, formattedModelValue);
}
/// <summary>
/// Custom version of Html.Editor for specific use in this project to generate the Model Meta Data from
/// the properties
/// </summary>
/// <param name="html"></param>
/// <param name="definition"></param>
/// <returns></returns>
public static HtmlString CoopTextBox(this IHtmlHelper html, string name, object value, IDictionary<string, object> htmlAttributes)
{
return CoopEditorCommon(html, name, value, htmlAttributes, null, (modelMetadata, formattedModelValue) =>
{
var result = _inputHelper2(html, InputType.Text, modelMetadata, "", formattedModelValue, false, false, true, true, null, htmlAttributes);
return result;
/*
return (MvcHtmlString)_inputHelper.Invoke(obj: null, parameters: new object[] {
html, InputType.Text, modelMetadata, "", formattedModelValue, false, false, true, true, null, htmlAttributes
});
*/
});
}
public static HtmlString CoopTextBox(this IHtmlHelper html, string name, object value, Definition definition, IDictionary<string, object> htmlAttributes)
{
return CoopEditorCommon(html, name, value, htmlAttributes, definition, (modelMetadata, formattedModelValue) =>
{
var result = _inputHelper2(html, InputType.Text, modelMetadata, name, formattedModelValue, false, false, true, true, null, htmlAttributes);
return result;
});
}
public static HtmlString CoopCheckBoxFor(this IHtmlHelper html, string name, object value, IDictionary<string, object> htmlAttributes)
{
return CoopEditorCommon(html, name, value, htmlAttributes, null, (modelMetadata, formattedModelValue) =>
{
return (HtmlString)_checkBoxHelper.Invoke(obj: null, parameters: new object[] {
html, modelMetadata, "", formattedModelValue, htmlAttributes
});
});
}
public static HtmlString CoopCheckBoxFor(this IHtmlHelper html, string name, object value, Definition definition, IDictionary<string, object> htmlAttributes)
{
return CoopEditorCommon(html, name, value, htmlAttributes, definition, (modelMetadata, formattedModelValue) =>
{
return (HtmlString)_checkBoxHelper.Invoke(obj: null, parameters: new object[] {
html, modelMetadata, name, formattedModelValue, htmlAttributes
});
});
}
public static HtmlString CoopMultiLineBox(this IHtmlHelper html, string name, object value, IDictionary<string, object> htmlAttributes)
{
var definition = html.ViewData["Definition"] as Definition;
return html.CoopEditorCommon(name, value, htmlAttributes, null, (modelMetadata, formattedModelValue) =>
{
modelMetadata = (ModelMetadata)formattedModelValue;
\#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
return (HtmlString)\_textAreaHelper.Invoke(obj: null, parameters: new object\[\] {
html, modelMetadata, "", null, htmlAttributes, null
});
\#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
//return (MvcHtmlString)method.Invoke(obj: null, parameters: new object\[\] {
// html, InputType.Text, modelMetadata, "", formattedModelValue, false, false, true, true, null, htmlAttributes
//});
});
}
public static HtmlString CoopMultiLineBox(this IHtmlHelper html, string name, object value, Definition definition, IDictionary<string, object> htmlAttributes)
{
return html.CoopEditorCommon(name, value, htmlAttributes, definition, (modelMetadata, formattedModelValue) =>
{
// modelMetadata.Model = formattedModelValue;
modelMetadata = (ModelMetadata)formattedModelValue;
return (HtmlString)_textAreaHelper.Invoke(obj: null, parameters: new object[] {
html, modelMetadata, name, null, htmlAttributes, null
});
});
}
}
}
Upvotes: 1
Views: 53