# 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();
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gapvelocity.ai/vbuc/knowledge-base/how-to/avoid-reflection-in-hot-paths.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
