English 中文(简体)
How to make Delphi Prism indexed properties visible to C# when properties are not default
原标题:

I have several Delphi Prism classes with indexed properties that I use a lot on my C# web applications (we are migrating a big Delphi Win32 system to ASP.Net). My problem is that it seems that C# can t see the indexed properties if they aren t the default properties of their classes. Maybe I m doing something wrong, but I m completely lost.

I know that this question looks a lot like a bug report, but I need to know if someone else knows how to solve this before I report a bug.

If I have a class like this:

TMyClass = public class
private
  ...
  method get_IndexedBool(index: Integer): boolean;
  method set_IndexedBool(index: Integer; value: boolean);
public
  property IndexedBool[index: Integer]: boolean
        read get_IndexedBool
        write set_IndexedBool; default; // make IndexedBool the default property
end;

I can use this class in C# like this:

var myObj = new TMyClass();

myObj[0] = true;

However, if TMyClass is defined like this:

TMyClass = public class
private
  ...
  method get_IndexedBool(index: Integer): boolean;
  method set_IndexedBool(index: Integer; value: boolean);
public
  property IndexedBool[index: Integer]: boolean
        read get_IndexedBool
        write set_IndexedBool; // IndexedBool is not the default property anymore
end;

Then the IndexedBool property becomes invisible in C#. The only way I can use it is doing this:

var myObj = new TMyClass();

myObj.set_IndexedBool(0, true);

I don t know if I m missing something, but I can t see the IndexedBool property if I remove the default in the property declaration. Besides that, I m pretty sure that it is wrong to have direct access to a private method of a class instance.

Any ideas?

最佳回答

I believe that C# 4.0 will support indexed properties but that anything before that will sadly not.

Unfortunately, what you are asking for is a limitation of C# and Not Delphi Prism. From the Delphi Prism Documentation wiki page on Delphi Prism vs C#:

C# can only access the default indexed properties. In Delphi Prism, you can define and use other indexed properties using their name.

This page also outlines other areas where Delphi Prism code includes unique or extended features over C# which might be useful in your port.

问题回答

It s unfortunate but that s the only way C# lets you access index properties. It s a C# compiler limitation (vb.net should do it fine).

Not beautiful or clean, but as a fast workaround you could make the indexed property accessors public. That way you can use them from C# to access the values.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签