> For the complete documentation index, see [llms.txt](https://docs.gapvelocity.ai/vbuc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gapvelocity.ai/vbuc/knowledge-base/how-to/avoid-reflection-in-hot-paths.md).

# Avoid reflection in Hot Paths

## Performance

Reflection allows to dynamically create an instance of a type, or get the type from an existing object and invoke its methods or access its fields and properties.

Reflection is very powerful but expensive.

Use reflection only when necessary, for example, when creating a COM object.

```csharp
Activator.CreateInstance("MyCOMObject");
```

Avoid using it in performance-sensitive scenarios and hot paths. In these cases, try to use strong types and call the methods directly.&#x20;

This method invokes the *MyMethod* method of the object *myObject* using reflection.

```csharp
public string MyMethod(object myObject)
{
    return myObject.GetType().GetMethod("MyMethod").
            Invoke(myObject,null).ToString();
}
```

In this case, the type MyClass is known so it replaces the *object* and the *MyMethod* method can be called directly.

```csharp
public string MyMethod(MyClass myObject)
{
    return myObject.MyMethod();
}
```
