LINQ makes my obfuscator break

I have the following LINQ which is causing my obfuscator to break.

.Where(f => f.FileName == fileName).OrderByDescending(f => f.Position).FirstOrDefault();

      

Is there any other way I could rewrite this LINQ statement to test against my obfuscator?

I reported a bug, but it may take 1-2 months, so I need to try to recode this LINQ in the meantime.

Update:

Exact reason in LINQ:

.Where(f => f.FileName == fileName)

      

+2


a source to share


4 answers


How exactly does the problem manifest? Since the C # expression compiler (used in LINQ above) uses memberinfo tokens directly (instead of relying on strings as reflection), I don't see what you can do better. Likewise, assuming it is an IL obfuscator (not the source of the obfuscator), rewriting it as a query expression, it should achieve nothing, nix, zip, zero and nada. You can try...

var first = (from f in [whatever]
             where f.FileName == fileName
             orderby f.Position descending
             select f).FirstOrDefault();

      

What exactly is going on?


Edit based on comment: if the problem is "capturing", you can try to manually create an expression with a constant (instead of the captured value) - where Foo

is your type:

    var param = Expression.Parameter(typeof(Foo), "f");
    var body = Expression.Equal(Expression.PropertyOrField(param, "FileName"),
        Expression.Constant(filename));
    var predicate = Expression.Lambda<Func<Foo, bool>>(body, param);

      



then use:

    .Where(predicate).OrderByDescending(f => f.Position).FirstOrDefault();

      

The problem is of course convincing that "FileName" and Foo.FileName

should remain unchanged ...


And here's a version that doesn't need a line:

    Expression<Func<Foo, string>> liftFileName = foo => foo.FileName;
    var predicate = Expression.Lambda<Func<Foo, bool>>(
        Expression.Equal(liftFileName.Body, Expression.Constant(filename)),
        liftFileName.Parameters);

      

+3


a source


In the presence of a broken instrument, a guaranteed fix cannot be provided. You have to work hard. Try replacing Where

and OrderBy...

. Possibly FirstOrDefault()

breaks, so maybe do this part by hand. In fact, it OrderBy...

can even be replaced with sorting. There are tons of other things you could try.



By the way, is this LINQ-to-SQL or LINQ-to-Objects? They behave differently.

0


a source


The solution is to exclude the .FileName and .Position properties from obfuscation (renaming).

To avoid having to do this on every request, switch to an obfuscator that supports LINQ-to- *, such as Crypto Obfuscator .

0


a source


Old post, but I had this problem this morning. I am using .NET Reactor which does not support my POCO to LINQ trick for entities.

If your obfuscator respects System.Reflection.ObfuscationAttribute

, you can use it to exclude entity and DbContext entities.

<Obfuscation(ApplyToMembers:=True, Exclude:=True)>

      

If you have heavier domain objects with different code that you would like to obfuscate, minimal exceptions are as follows:

  • DbContext class name
  • Property name for each DbSet
  • Entity class name
  • Properties of mapped objects

In these cases, you only need to use the lighter form.

<Obfuscation(Exclude:=True)> Public Property ID As Integer

      

0


a source







All Articles