C # cast exception in C # dll

I have a class with a field that is assigned an array of type T or a dll object of type T that has an overload this [int i]to simulate an array (it uses unmanaged memory and accesses it using overload)

    public T this[int i]
    {
        get
        {
            return ((T[])array)[i]; // can't use return array[i] directly
        }
        set { }
    }

when I use the dll object, it throws an exception exception. I suggested that it (T[])would cause the []object to be overloaded , not casting on float[], but it always adds to float[], but the type of object FloatArrthat has public float this[int i].

Is there a way to treat primitive arrays as casts, treating other custom types as an overload trigger?

If this is not possible, how to return the type T, while a returned type, such as float, can be assembled from a simple float array or some non-standard type with overloading to return a float? I do not want to add float, int, byte, double, char, ... one by one. I need the type to Twork for everyone, is the object field an C # array or a custom type with an overloaded index?

What I really need:

    // developer supplies here with his/hers own array
    // or this library uses its own fast C++ wrapper with a [] overload
    public object array { get; set; }

    // returns float, int, byte
    public T this[int i]
    {
        get
        {
            return array[i]; // float[], int[], FloatArr[], ...
        }
        set { }
    }

Edit: if this is a bad design, please tell me. I need this class to consider a property, be it a C # array or a C ++ shell Cll object with overload [].

Edit-2: Here is what I used in my mind:

MyArr<float> gpu = new MyArr<float>(1024);
gpu.array=new float[1024];
gpu[35]=40;

or


MyArr<float> gpu = new MyArr<float>(1024);
gpu.allocateUnManagedToArray(); // now array field is a custom object
gpu[35]=40;

Edit-3: here is the custom class that I put in the array field:

public class FloatArr
{
    public float this[int i]
    {
        get
        {
            unsafe
            {
                float* p = (float*)hArr.ToPointer();
                return *(p + i);
            }

        }
        set {
            unsafe
            {
                float* p = (float*)hArr.ToPointer();
                *(p + i) = value;
            }
        }
    }
}

-4: :

  • []
  • , , []!
  • , #, , ++.
  • [] ( float ++ []).

, float int byte , . , , - , . struct, , .

+4
2

, , IList<T> . , .

class FloatArr : IList<float>
{
    //(snip)
    // p and hArr decliration and assignment
    //(snip)

    public float this[int index]
    {
        get
        {
            unsafe
            {
                float* p = (float*)hArr.ToPointer();
                return *(p + i);
            }

        }
        set
        {
            unsafe
            {
                float* p = (float*)hArr.ToPointer();
                *(p + i) = value;
            }
        }
    }

    IEnumerator<float> IEnumerable<float>.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    void ICollection<float>.Add(float item)
    {
        throw new NotImplementedException();
    }

    void ICollection<float>.Clear()
    {
        throw new NotImplementedException();
    }

    bool ICollection<float>.Contains(float item)
    {
        throw new NotImplementedException();
    }

    void ICollection<float>.CopyTo(float[] array, int arrayIndex)
    {
        throw new NotImplementedException();
    }

    bool ICollection<float>.Remove(float item)
    {
        throw new NotImplementedException();
    }

    int ICollection<float>.Count
    {
        get { throw new NotImplementedException(); }
    }

    bool ICollection<float>.IsReadOnly
    {
        get { throw new NotImplementedException(); }
    }

    int IList<float>.IndexOf(float item)
    {
        throw new NotImplementedException();
    }

    void IList<float>.Insert(int index, float item)
    {
        throw new NotImplementedException();
    }

    void IList<float>.RemoveAt(int index)
    {
        throw new NotImplementedException();
    }
}

, , IReadOnlyList<T>

class FloatArr : IReadOnlyList<float>
{
    //(snip)
    // p and hArr decliration and assignment
    //(snip)

    public float this[int index]
    {
        get
        {
            unsafe
            {
                float* p = (float*)hArr.ToPointer();
                return *(p + i);
            }

        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator<float> IEnumerable<float>.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    int IReadOnlyCollection<float>.Count
    {
        get { throw new NotImplementedException(); }
    }
}

// developer supplies here with his/hers own array
// or this library uses its own fast C++ wrapper with a [] overload
public IList<T> array { get; set; }

// returns float, int, byte
public T this[int i]
{
    get
    {
        return array[i]; // float[], int[], FloatArr, ...
    }
    set
    {
        array[i] = value;
    }
}

// developer supplies here with his/hers own array
// or this library uses its own fast C++ wrapper with a [] overload
public IReadOnlyList<T> array { get; set; }

// returns float, int, byte
public T this[int i]
{
    get
    {
        return array[i]; // float[], int[], FloatArr, ...
    }
}
+1

IList<T> IReadOnlyList<T> dynamic . , IList<T> IReadOnlyList<T>

// developer supplies here with his/hers own array
// or this library uses its own fast C++ wrapper with a [] overload
public dynamic array { get; set; }

// returns float, int, byte
public T this[int i]
{
    get
    {
        return array[i]; // float[], int[], FloatArr[], ...
    }
    set 
    {
        array[i] = value;
    }
}
+1

Source: https://habr.com/ru/post/1672868/


All Articles