首页 \ 问答 \ 从堆栈中的类获取泛型参数(Getting generic arguments from a class in the stack)

从堆栈中的类获取泛型参数(Getting generic arguments from a class in the stack)

我有一个名为Repository的泛型类。 该类有一个函数,通过使用不同的泛型参数初始化Repository类的新实例来“调用自身”。 这个“递归”可以继续 - 所以为了避免StackOverflowException,我需要检查堆栈中是否存在一个从Repository类调用的具有相同泛型参数的方法。 这是我的代码:

    StackTrace stack = new StackTrace();
    StackFrame[] frames = stack.GetFrames();

    foreach (StackFrame frame in frames)
    {
        Type callingMethodClassType = frame.GetMethod().DeclaringType;
        if (callingMethodClassType.IsGenericType)
        {
            // BUG HERE in getting generic arguments of the class in stack
            Type genericType = callingMethodClassType.GetGenericArguments()[0];
            if (genericType.Equals(entityType))
            {
                wasAlready = true;
                break;
            }
        }
    }

泛型类型始终返回为T而不是正确的类型,如“User”或“Employee”(例如)。 我无法比较类型的名称,因为T没有名称。


I have a generic class called Repository. This class has a function that "calls itself" by initializing a new instance of the Repository class with a different generic argument. This "recursion" can go on - so to avoid StackOverflowException, i need to check if there is in the stack, a method called from the Repository class with the same generic argument. here is my code:

    StackTrace stack = new StackTrace();
    StackFrame[] frames = stack.GetFrames();

    foreach (StackFrame frame in frames)
    {
        Type callingMethodClassType = frame.GetMethod().DeclaringType;
        if (callingMethodClassType.IsGenericType)
        {
            // BUG HERE in getting generic arguments of the class in stack
            Type genericType = callingMethodClassType.GetGenericArguments()[0];
            if (genericType.Equals(entityType))
            {
                wasAlready = true;
                break;
            }
        }
    }

the generic type always returns as T and not the correct type like "User" or "Employee" (for example). I can't compare the names of the types because T does not have a name.


原文:https://stackoverflow.com/questions/6250931
更新时间:2022-02-13 16:02

最满意答案

.blur()按照您所期望的选定倍数工作。 如果它不适合你,你的代码中必须有某种错误。

如果您发布代码,有人可能会帮助您,但目前,我只能说.blur()是要走的路。


.blur() works as you would expect on select multiples. If it's not functioning for you, you must have some sort of error in your code.

If you post your code, someone might be able to help you, but for the moment, all I can say is that .blur() is the way to go.

相关问答

更多
  • .blur()按照您所期望的选定倍数工作。 如果它不适合你,你的代码中必须有某种错误。 如果您发布代码,有人可能会帮助您,但目前,我只能说.blur()是要走的路。 .blur() works as you would expect on select multiples. If it's not functioning for you, you must have some sort of error in your code. If you post your code, someone might b ...
  • 不使用背景颜色。 我认为在这种情况下唯一的方法是使用一些javascript-plugin来制作自定义元素并使用底层选择。 例如: http : //designwithpc.com/Plugins/ddSlick Not using background-colors. I think the only way in this case is to use some javascript-plugin that makes a custom element and uses an underlaying ...
  • 你可以试试这个: function selectAll(select) { if (select == false) { $("#sel").val([]); } else { $("#sel option").prop("selected", true); } $("#sel").selectmenu("refresh", true); } 这是一个演示 。 You can try this: function selectAll(se ...
  • 在angular添加/更改选项组和选项后,您需要告诉jQM刷新selectmenu小部件: $("#multiselect").selectmenu("refresh", true); API Doc: http : //api.jquerymobile.com/selectmenu/#method-refresh After angular has added/changed the optiongroups and options, you need to tell jQM to refresh th ...
  • 那是因为你在每次迭代中覆盖了之前选择的值,你应该将数组传递给val方法,试试这个: var vals = []; for (i=0; i<=data[0].fields['sales_item'].length;i++) { vals.push(data[0].fields['sales_item'][i]); } $("#id_deals-sales_item").val(vals); That's because you are overriding previous selected v ...
  • 添加动态创建的内容时,必须使用正确的jQuery Mobile样式进行增强。 在这种情况下,这将工作: $('#pg_invoicedtl').trigger('pagecreate'); 或完整的JavaScript: $(document).ready(function(){ $('#appeprod').click(function() { var option = $('#prodselect').val(); var box = document.getE ...
  • 我将使用数据属性来存储相关多框的id,而不是将其置于位置(下一个可用的多框)。
    select all
    然后在你的脚本中: