Skip to content

Types

The ByteCobra.Reflection.TypeExtensions class provides extension methods for working with types. These methods enable you to perform various checks and operations related to types. This class is designed to simplify common type-related tasks, such as checking if a given type implements a specified interface or retrieving methods of a type with specific criteria.

The ByteCobra.Reflection.TypeExtensions class is a convenient way to perform type-related operations and checks with a simple and readable syntax. It helps make type-related code more concise and can be especially useful when working with dynamically loaded types or when you need to check type compatibility at runtime.

Please note that the extension methods provided by ByteCobra.Reflection.TypeExtensions are designed to be used in a broader context and can be combined with other reflection or type-related features to achieve more advanced functionality in your applications.

It is important to ensure that you provide correct type parameters when using these extension methods to avoid exceptions at runtime. Additionally, consider handling any potential null or edge cases appropriately when using these methods to ensure the robustness of your code.

The extension methods provided by this class are read-only operations that provide valuable insights into types and their relationships, making them a powerful addition to your type-related toolbox.

Methods

Implements

Determines whether the current type implements the specified interface type.

Parameters

  • type: The current type.
  • interfaceType: The interface type to check for implementation.

Returns: true if the current type implements the specified interface type; otherwise, false.

Remarks: This method checks if the current type implements the specified interface type. It retrieves all interfaces implemented by the current type using the GetInterfaces() method and then checks if the specified interface type is present in the list of interfaces.

Example:

using UnityEngine;

namespace ByteCobra.Reflection.Example
{
    public interface IExampleInterface
    {
        void InterfaceMethod();
    }

    public class ExampleMonoBehaviour : MonoBehaviour, IExampleInterface
    {
        public void InterfaceMethod()
        {
            Debug.Log("Interface method called!");
        }
    }
}

// Usage:
Type type = typeof(ByteCobra.Reflection.Example.ExampleMonoBehaviour);
Type interfaceType = typeof(ByteCobra.Reflection.Example.IExampleInterface);

bool result = type.Implements(interfaceType);
// result will be true if ExampleMonoBehaviour implements IExampleInterface; 
// otherwise, false.

GetMethodInfo

Gets an enumerable of MethodInfo instances representing the methods of the specified type.

Parameters

  • type: The Type for which to retrieve methods.
  • flags: Optional BindingFlags to customize method retrieval. If not provided, defaults to BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static.
  • inherited: Specifies whether to include inherited methods from base types.

Returns: An enumerable of MethodInfo instances representing the methods of the specified type.

Remarks: This method retrieves methods declared by the specified type and optionally its base types.

Example:

using System;
using System.Reflection;
using UnityEngine;

namespace ByteCobra.Reflection.Example
{
    public class ExampleMonoBehaviour : MonoBehaviour
    {
        public void SomeMethod()
        {
            Debug.Log("SomeMethod called!");
        }
    }
}

// Usage:
Type type = typeof(ByteCobra.Reflection.Example.ExampleMonoBehaviour);
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
bool inherited = true;

var methods = type.GetMethodInfo(flags, inherited);
foreach (var method in methods)
{
    Debug.Log("Method Name: " + method.Name);
}

// Output:
// Method Name: SomeMethod

Retrieving Members with Specific Attributes

These methods allow you to retrieve various members of a type (events, methods, properties, and fields) that are decorated with a specific attribute. For each member that has the specified attribute, the methods return a tuple containing the member's reflection object and the attribute instance.

GetMethodsWithAttribute<T>

Returns an enumerable of tuples, each containing a MethodInfo object and an attribute of the specified type T.

Below is an example of how to use the GetMethodsWithAttribute extension method. In this example, we have a custom attribute named CustomMethodAttribute, a MonoBehaviour named ExampleMonoBehaviour, and we demonstrate how to retrieve methods decorated with CustomMethodAttribute from this MonoBehaviour.

Example

using System;

[AttributeUsage(AttributeTargets.Method)]
public class CustomMethodAttribute : Attribute
{
    public string Description { get; }

    public CustomMethodAttribute(string description)
    {
        this.Description = description;
    }
}
using UnityEngine;

public class ExampleMonoBehaviour : MonoBehaviour
{
    [CustomMethod("This is a custom method 1")]
    public void CustomMethod1()
    {
        Debug.Log("CustomMethod1 called!");
    }

    [CustomMethod("This is a custom method 2")]
    public void CustomMethod2()
    {
        Debug.Log("CustomMethod2 called!");
    }

    public void RegularMethod()
    {
        Debug.Log("RegularMethod called!");
    }
}
using System;
using System.Reflection;
using UnityEngine;

public class AttributeUsageExample : MonoBehaviour
{
    void Start()
    {
        Type type = typeof(ExampleMonoBehaviour);

        var methodsWithAttribute = type.GetMethodsWithAttribute<CustomMethodAttribute>();
        foreach (var (method, attribute) in methodsWithAttribute)
        {
            Debug.Log(attribute.Description);
        }
    }
}

Output

This is a custom method 1 
This is a custom method 2

GetPropertiesWithAttribute<T>

Returns an enumerable of tuples, each containing a FieldInfo object and an attribute of the specified type T.

GetEventsWithAttribute<T>

Returns an enumerable of tuples, each containing an EventInfo object and an attribute of the specified type T.