2

In a class definition, I implemented IList<T> to make it look like an array.

    // Foo has C++ arrays inside for a 
    // fast communication with some hardware
    public abstract class Foo<T>  : IList<T(or uint for a derived class)>
    {
        public virtual void CopyTo(uint[] array, int arrayIndex)
        {
            int dL = Length;
            if (dL == array.Length)
            {
                /* needs pinning the target before this?*/
                Marshal.Copy(handleForFooUnmanagedArray,
                            (int[])(object) array, 
                            arrayIndex, 
                            dL - arrayIndex);
                return;
            }
            throw new NotImplementedException();
        }
    }

so it can do this now:

 uint [] bar = new uint[L];
 foo.CopyTo(bar,0);

but now I want to make it work like an array with this:

 uint [] bar = new uint[L];
 bar.CopyTo(foo,0);

so I looked what interfaces an array implements in run-time(here) to find something like a private .CopyFrom that I thought should be called implicity in `.CopyTo',

  • IList
  • ICloneable
  • ICollection
  • IEnumerable
  • IStructuralComparable
  • IStructuralEquatable

non of these have any .CopyFrom.

Maybe there is some IntPtr property as a handle for Marshal copying in .CopyTo but I couldn't see it in intellisense.

Question:

How can I find that which method does the .CopyTo use to get necessary info about target array and what that necessary info would that be? Another method like a .CopyFrom or a handle pointing to start of target array, or some interpreter intermediate codes stored in somewhere? Is the target array pinned in the process?

Side question:

Do I need to implement some extra methods in IList<T> on top of important(unknown) ones?

I already implemented toArray, Count and [] but I havent done anything for others yet. Then Foo also has Length(with a custom interface) but it doesn't belong Array so an uint[] may not use it in its CopyTo.

I'm not experienced with IL so I may not understand if thats the solution but I can look back in time.

Also I tried to implement Array which refuses to be implemented because of being a special class.

Thank you very much for your time.

Community
  • 1
  • 1
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
  • I don't know, I just need to know what an uint array does in its own `CopyTo` to copy its data to Foo (if Foo is correctly acts like an array but infact can't implement Array since it is special class) – huseyin tugrul buyukisik Mar 24 '17 at 21:46
  • Well `CopyTo` is implemented in unmanaged code inside runtime itself, so you won't find (at least not easily) what exactly it expects. Does your Foo class contains managed array somewhere inside? Or only unmanaged C++ array? – Evk Mar 24 '17 at 21:51
  • @Evk it has only unmanaged but having a managed one seems like a good idea, then I would overload the implicit conversion to give that instead of itself then it would work.(at the time of using its data, it will check array data changed signs) But with the expense of duplicated memory. But who cares. Thanks. If you write this in question, I accept. Next question will be, how to know if array is changed, or referenced(maybe already asked idk) – huseyin tugrul buyukisik Mar 24 '17 at 21:54
  • Then only a `CopyFrom` will be needed for Foo. But this will be ofcourse slower version of directly calling `CopyFrom` in developer code – huseyin tugrul buyukisik Mar 24 '17 at 21:59

1 Answers1

2

CopyTo is implemented in unmanaged code by runtime itself, and signature of method looks like this:

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable);

As you see it still expects Array and not some pointer, so it's hard to do what you want.

But if you can have a managed array inside your Foo then it's easy to achieve the goal - just use implicit conversion to Array like this:

class MyFakeArray {
    uint[] _realArray = new uint[10];

    public MyFakeArray() {

    }

    public static implicit operator uint[](MyFakeArray a) {
        return a._realArray;
    }
}

Then CopyTo will work as expected:

var a = new uint[10];
var fa = new MyFakeArray();
a.CopyTo(fa, 0);
Evk
  • 98,527
  • 8
  • 141
  • 191
  • I couldn't find any solution for "how to know if array has changed" but putting a counter increment command into implicit operator should "fix" it for Foo. Should I ask it anyway? – huseyin tugrul buyukisik Mar 24 '17 at 22:06
  • Could "adding a dedicated thread, for concurrent copy from managed to unmanaged, only if array has changed, until Foo is used again" be any good for a gpgpu library structure? – huseyin tugrul buyukisik Mar 24 '17 at 22:10
  • Well only you know that I'm suppose :) Do you have a use already for such structure? Though if implicit operator is called does not mean array will necessary be changed. – Evk Mar 24 '17 at 22:12
  • I will try an tell you if computer doesnt explode with some IL magic. Gpgpu part is already working, I'm just making user side arrays more flexible instead of forcing to use explicit c++ arrays or C# arrays. – huseyin tugrul buyukisik Mar 24 '17 at 22:18
  • it works! now should I ask it in codereview or here to make sure? – huseyin tugrul buyukisik Mar 24 '17 at 22:34
  • Well if you have working code - I think that is a question for code review. here you usually post when something is broken or you don't know how to make it work. – Evk Mar 24 '17 at 22:35
  • @huseyintugrulbuyukisik If the code works and you want others to review your code and offer suggestions or criticisms, then post it on Code Review. Otherwise, you would post on StackOverflow here if it's broken or don't know how to make it actually work. – Thomas Ward Mar 24 '17 at 22:36
  • thank you then I ask in code review for criticized review – huseyin tugrul buyukisik Mar 24 '17 at 22:38
  • http://codereview.stackexchange.com/questions/158773/is-this-pseudo-copyfrom-implementation-ok if you check, I appreciate. – huseyin tugrul buyukisik Mar 24 '17 at 23:06