Object pointers(object*) in C#

試著忘記壹切 提交于 2020-01-15 10:43:09

问题


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

回答1:


Maybe you should pass in a delegate that your class can use to obtain the “object string”.

class MyFancyClass
{
    Func<string> getObjectString;

    public MyFancyClass(Func<string> getObjectString)
    {
        this.getObjectString = getObjectString;
    }

    private MyOtherThread()
    {
        // ...
        string desc = getObjectString();
        // ...
    }
}

// ...
long value = 34;
MyFancyClass fancy = new MyFancyClass(() => value.ToString());

// ...
value = 88;
// getObjectString() should now reflect the new value.
// The variable is captured in the lambdas closure.

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.




回答2:


In C#, you usually don't use pointers. If you want to refer to a storage location, try this:

whatever(ref object variable)
{

}

Else, i would rather recommend using a wrapper class or another way to get to some variable. A wrapper might look like this:

class Wrapper
{
    public object Value { get; set; }
}



回答3:


If you want to get the address of a variable it can't be a complex managed type. However, you can do something like this (assuming an unsafe context):

int a = 1;

IntPtr addr = ( IntPtr )( &a );

( * ( int* ) addr ) = 4;

a = 4;

Is this what you're looking for?

Edit:

If you're just wanting to save a "function pointer" from the object, just use a delegate to hold that.

class whatever
{
    public whatever(object variable)
    {
        getVariableValue = variable.ToString;
    }

    Func<string> getVariableValue;

    public override string ToString()
    {
        return getVariableValue();
    }
}

void Main()
{
    var type = new { F1 = "test", F2 = "value" };

    whatever w = new whatever( type );

    Console.WriteLine( w ); // This will invoke ToString
}

Output: { F1 = test, F2 = value }




回答4:


You want a void* but becareful with those. if you make the wrong cast it will crash. it might be a wise idea to make a struct and cast to an intptr in this case.



来源:https://stackoverflow.com/questions/6455252/object-pointersobject-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!