C#里:
public class A
    {
        int[] nodes = new int[] { };
        public int this[int index]
        {
            get
            {
                return nodes[index];
            }
            set
            {
                nodes[index] = value;
            }
        }
    }    class B : A
    {
        //.....        //这里要访问父类A里的nodes.Length,该怎么做? A里的nodes只能是私有的,不可以改了。有其他办法么?        //可以通过A类里那个this[]属性来访问到nodes的长度么? 怎么写? 谢谢
    }

解决方案 »

  1.   

    新增一个属性,公开nodes.Length

    public int Length{get{return nodes.Length;}}
      

  2.   

    要么加个属性来访问 nodes,要么把nodes的 private 改为 protected 或 public!否则滴话 lz 就是在自找麻烦!
      

  3.   

    楼上,我已经已经不能那样做了,  如果我能修改父类的话,我还不如直接将父类的nodes设为public呢,
      要想办法通过this[]哪个属性访问
      

  4.   

    //这里要访问父类A里的nodes.Length,该怎么做? A里的nodes只能是私有的,不可以改了。有其他办法么?
    //可以通过A类里那个this[]属性来访问到nodes的长度么? 怎么写? 谢谢那就把 A 里 nodes 的 private 改为 protected,这样在 B 中可以访问 base.nodes.Length,
    但外部无法访问到 nodes!
      

  5.   

    A是一个dll类里的,我只有dll,而且加密了  不可能修改的
      

  6.   

    this[] 只是索引,访问不到 nodes 滴!不改 A 是不可能滴!
      

  7.   

    还有其它接口吗?怎么给INT[] 数组赋值的呢?
      

  8.   

    用不着什么继承来获取啊,你只需要实例化这个类A,然后测试一下它的最大索引是多少,就知道相应的nodes一共有多少项了
      

  9.   

    除非遍历L@_@K
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApp
    {
        public class A
        {
            int[] nodes = new int[] { };
            public int this[int index]
            {
                get
                {
                    return nodes[index];
                }
                set
                {
                    nodes[index] = value;
                }
            }        public A(int length)
            {
                nodes = new int[length];
            }
        }    class B : A
        {
            public int NodeLength
            {
                get
                {
                    int length = 0;
                    int value;
                    try
                    {
                        for (; ; length++)
                        {
                            value = base[length];
                        }
                    }
                    catch { }                return length;
                }
            }        public B(int length) : base(length) { }
        }    class Program
        {
            static void Main(string[] args)
            {
                B b = new B(5);
                b[0] = 1;
                b[1] = 2;            Console.WriteLine(b.NodeLength);
                Console.ReadLine();
            }
        }
    }
      

  10.   

     在Main中:
                  A test = new A();
                bool IsError = false;
                int length = 0;
                while(!IsError)
                try
                { 
                  int n = test[3]; 
                }
                catch (Exception e)
                { 
                IsError = true;
                length = length + 1;
                }
                Console.WriteLine(length);//这个就是你要的length
                Console.ReadLine();
      

  11.   

    int[] nodes = new int[] { };
    这么写是空数组!!!
      

  12.   

    int[]怎么赋值的?如果有继承到的方法在B中重写,那就在B里边计算length了
      

  13.   

    不好意思,上面那个错了:应该是:
    Console.WriteLine(nodes.Length);
                A test = new A();
                bool IsError = false;
                int length = 0;
                while(!IsError)
                try
                {
                    length = length + 1;
                    int n = test[length]; 
                }
                catch (Exception e)
                { 
                IsError = true;
                length = length - 1;
                }
                Console.WriteLine(length);
                Console.ReadLine();当然,上面哥们说的是,
    int[] nodes = new int[] { };这么写是空数组!!!