문제

My Code are as follows:

public static unsafe bool ExistInURL(params object[] ob)
    {
        void* voidPtr = (void*) stackalloc byte[5];//Invalid expression term
        *((int*) voidPtr) = 0;
        while (*(((int*) voidPtr)) < ob.Length)
        {
            if ((ob[*((int*) voidPtr)] == null) || (ob[*((int*) voidPtr)].ToString().Trim() == ""))
            {
                *((sbyte*) (voidPtr + 4)) = 0;//Cannot apply operator '+' to operand of type 'void*' and 'int'
                goto Label_004B;
            }
            *(((int*) voidPtr))++;//The left-hand side of an assignment must be a varibale,property or indexer
        }
        *((sbyte*) (voidPtr + 4)) = 1;
    Label_004B:
        return *(((bool*) (voidPtr + 4)));//Cannot apply operator '+' to operand of type 'void*' and 'int'
    }

The problem is when i'm trying to build or run the project i get a lot of errors. Anyone have any ideas?

도움이 되었습니까?

해결책

Your code (corrected):

public static unsafe bool ExistInURL(params object[] ob)
{
    byte* voidPtr = stackalloc byte[5];
    *((int*)voidPtr) = 0;

    while (*(((int*)voidPtr)) < ob.Length)
    {
        if ((ob[*((int*)voidPtr)] == null) || (ob[*((int*)voidPtr)].ToString().Trim() == ""))
        {
            *((sbyte*)(voidPtr + 4)) = 0;
            goto Label_004B;
        }

        (*(((int*)voidPtr)))++;
    }
    *((sbyte*)(voidPtr + 4)) = 1;

Label_004B:
    return *(((bool*)(voidPtr + 4)));
}

instead of void* use byte*, stackalloc return mustn't be casted, the ++ operator requires another set of ()... Your decompiler has some bugs :-) You should tell this to the authors.

and the unobfuscated version of your code:

public static bool ExistInURL2(params object[] ob)
{
    int ix = 0;
    bool ret;

    while (ix < ob.Length)
    {
        if (ob[ix] == null || ob[ix].ToString().Trim() == string.Empty)
        {
            ret = false;
            goto Label_004B;
        }

        ix++;
    }

    ret = true;

Label_004B:
    return ret;
}

(I left the goto... but it isn't really necessary)

Without the goto:

public static bool ExistInURL3(params object[] ob)
{
    int ix = 0;

    while (ix < ob.Length)
    {
        if (ob[ix] == null || ob[ix].ToString().Trim() == string.Empty)
        {
            return false;
        }

        ix++;
    }

    return true;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top