小编ldp*_*ldp的帖子

为什么Marshal.WriteInt64方法的代码如此复杂?

以下代码反映在.Net Framework中:

[SecurityCritical]
public static unsafe void WriteInt64(IntPtr ptr, int ofs, long val){
    try{
        byte* numPtr = (byte*) (((void*) ptr) + ofs);
        if ((((int) numPtr) & 7) == 0){
            *((long*) numPtr) = val;
        }
        else{
            byte* numPtr2 = (byte*) &val;
            numPtr[0] = numPtr2[0];
            numPtr[1] = numPtr2[1];
            numPtr[2] = numPtr2[2];
            numPtr[3] = numPtr2[3];
            numPtr[4] = numPtr2[4];
            numPtr[6] = numPtr2[6];
            numPtr[7] = numPtr2[7];
        }
    }
    catch (NullReferenceException){
        throw new AccessViolationException();
    }
}
Run Code Online (Sandbox Code Playgroud)

在我看来,*((long*) numPtr) = val足够,而且效率很高.

为什么这么复杂?

c# unsafe marshalling

5
推荐指数
1
解决办法
170
查看次数

JQuery:自闭div

我正在反对一个遗留系统,当 div 不包含任何内容时,它会产生自动关闭的 div。

我想使用 jQuery 来获取某些 div 的内部 html,但是如果 div 是自关闭的,jQuery 总是会得到错误的 html。

请参阅下面的演示代码:

<!doctype html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="application/javascript">
        $(function () {
            var s = $('.b').html();
            alert(s);
        });
    </script>
</head>
<body>
    <div class="a"></div>
    <div class="b" />
    <div class="c">This is c</div>
</html>
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我得到以下结果:

在此处输入图片说明

请帮助我,任何建议将不胜感激。

html jquery dom

5
推荐指数
1
解决办法
1365
查看次数

使用反射调用Enumerable.Where(或其他重载的泛型方法)

Enumerable类中的"Where"方法有2个重载(或方法签名):

namespace System.Linq {
    public static class Enumerable {
        public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
        public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);
    }
Run Code Online (Sandbox Code Playgroud)

所以

var where = typeof(Enumerable).GetMethod("Where") 
Run Code Online (Sandbox Code Playgroud)

抛出异常表明模糊匹配,因为当然,有多个名称为"Where"的方法,所以我试图通过参数区分:

var types = new[] { 
    typeof(IEnumerable<>), 
    typeof(Func<,>)};
var where = typeof(Enumerable).GetMethod("Where", types);
Run Code Online (Sandbox Code Playgroud)

然而,这与任何一种方法签名都不匹配,我不知道为什么.

广义问题:如何通过反射调用重载泛型方法而不迭代同名中具有相同名称的所有方法(即使用System.Type.GetMethod(System.String,System.Type [])?

请帮我解决一下!谢谢!

.net c# reflection extension-methods

4
推荐指数
1
解决办法
1547
查看次数

标签 统计

c# ×2

.net ×1

dom ×1

extension-methods ×1

html ×1

jquery ×1

marshalling ×1

reflection ×1

unsafe ×1