Repository: mandiant/capa
File: capa/features/extractors/dnfile/helpers.py (Python)

Code before:

        if not isinstance(member_ref.Class.row, dnfile.mdtable.TypeRefRow):
            # only process class imports from TypeRef table
            continue

        token: int = calculate_dotnet_token_value(dnfile.mdtable.MemberRef.number, rid)
        access: Optional[str]

        # assume .NET imports starting with get_/set_ are used to access a property
        if member_ref.Name.startswith("get_"):
            access = FeatureAccess.READ
        elif member_ref.Name.startswith("set_"):
            access = FeatureAccess.WRITE
        else:
            access = None

        member_ref_name: str = member_ref.Name
        if member_ref_name.startswith(("get_", "set_")):
            # remove get_/set_ from MemberRef name
            member_ref_name = member_ref_name[4:]

        typeref_name = []
        typerefname = (member_ref.Class.row.TypeName,)
        typerefnamespace = member_ref.Class.row.TypeNamespace
        if type(member_ref.Class.table) is dnfile.mdtable.TypeRef:
            if type(member_ref.Class.row.TypeName) is str:
                typerefnamespace, typerefname = typeref_helper(member_ref.Class.row_index, typeref_table, typeref_name, member_ref.Class.row.TypeName)

        yield DnType(
            token,
            typerefname,
            namespace=typerefnamespace,
            member=member_ref_name,
            access=access,
        )


def get_dotnet_methoddef_property_accessors(pe: dnfile.dnPE) -> Iterator[Tuple[int, str]]:
    """get MethodDef methods used to access properties

    see https://www.ntcore.com/files/dotnetformat.htm

    24 - MethodSemantics Table
        Links Events and Properties to specific methods. For example one Event can be associated to more methods. A property uses this table to associate get/set methods.
            Semantics (a 2-byte bitmask of type MethodSemanticsAttributes)
            Method (index into the MethodDef table)
            Association (index into the Event or Property table; more precisely, a HasSemantics coded index)
    """
    for rid, method_semantics in iter_dotnet_table(pe, dnfile.mdtable.MethodSemantics.number):
        assert isinstance(method_semantics, dnfile.mdtable.MethodSemanticsRow)


Review comment (refactor):
ruff linter doesn't like this:
```
capa/features/extractors/dnfile/helpers.py:140:16: E721 Do not compare types, use `isinstance()`
Found 1 error.
```

Code after:
            # only process class imports from TypeRef table
            continue

        token: int = calculate_dotnet_token_value(dnfile.mdtable.MemberRef.number, rid)
        access: Optional[str]

        # assume .NET imports starting with get_/set_ are used to access a property
        if member_ref.Name.startswith("get_"):
            access = FeatureAccess.READ
        elif member_ref.Name.startswith("set_"):
            access = FeatureAccess.WRITE
        else:
            access = None

        member_ref_name: str = member_ref.Name
        if member_ref_name.startswith(("get_", "set_")):
            # remove get_/set_ from MemberRef name
            member_ref_name = member_ref_name[4:]

        typerefnamespace, typerefname = resolve_nested_typeref_name(
            member_ref.Class.row_index, member_ref.Class.row, pe
        )

        yield DnType(
            token,
            typerefname,
            namespace=typerefnamespace,
            member=member_ref_name,
            access=access,
        )


def get_dotnet_methoddef_property_accessors(pe: dnfile.dnPE) -> Iterator[Tuple[int, str]]:
    """get MethodDef methods used to access properties

    see https://www.ntcore.com/files/dotnetformat.htm

    24 - MethodSemantics Table
        Links Events and Properties to specific methods. For example one Event can be associated to more methods. A property uses this table to associate get/set methods.
            Semantics (a 2-byte bitmask of type MethodSemanticsAttributes)
            Method (index into the MethodDef table)
            Association (index into the Event or Property table; more precisely, a HasSemantics coded index)
    """
    for rid, method_semantics in iter_dotnet_table(pe, dnfile.mdtable.MethodSemantics.number):
        assert isinstance(method_semantics, dnfile.mdtable.MethodSemanticsRow)

        if method_semantics.Association.row is None:
            logger.debug("MethodSemantics[0x%X] Association row is None", rid)
            continue

        if isinstance(method_semantics.Association.row, dnfile.mdtable.EventRow):