What I want to do is, I want to pass a pointer to a function that may be any type of a variable(int, long, string, even a class I mean I should be able to pass any variable’s pointer). I’m doing this like that
unsafe class whatever
{
whatever(object* variable)
{
this.variable = variable;
}
}
ERROR IS: Cannot take the address of, get the size of, or declare a pointer to a managed type (‘object’)
Why I want to do is, I will store the variables passed through the constructor and will use their ToString() method, I’m trying to make a class that is for console applications, Refreshing the variables with their updated variables.
If I could do it like that My code will look like that
unsafe class whatever
{
whatever(object* variable)
{
this.variable = variable;
}
object* variable;
public override string ToString()
{
return *variable.ToString();
}
}
Maybe you should pass in a delegate that your class can use to obtain the “object string”.
Be careful though, because the delegate is called from another thread and simply calling ToString() may not be safe for all objects and require locking. However a delegate allows the caller to do this, depending on the object.
Pointers will get ugly, require unsafe code and aren’t stable. The garbage collector can move objects around freely, if you don’t explicitly make the pointers fixed. References can’t be stored and you can only pass them around.