0

私のアプリケーションでは、mpxjを使用してMicrosoft Projectファイルからアイテムを取得しています。必要なアイテムの1つは、先行アイテムです。前任者を引っ張ることができる方法は、java.util.listのタイプを返すmpxjのビルドイン関数を使用することです-これをオブジェクトとして変数に保存できますが、データをにもたらす方法を見つける必要があります簡単に使用できるフォーマットなので、データベースに保存できます。以下にリストされているのは、プロジェクトファイルから先行タスクをプルするために使用しているコード行です。

Dim predecessors = task.getPredecessors

トレースポイントを入れて前任者の値を取得したときの結果は次のとおりです

[[Relation [Task id=4 uniqueID=45577 name=Standards Training - Round 2] -> [Task id=3 uniqueID=45576 name=Process Excellence Training]]]

上記を文字列として取得できたとしても、必要なデータを取得するのに十分な作業を行うことができました。上記の例では、先行リストに1つのアイテムがありますが、複数のアイテムがある場合があります。複数のアイテムがある場合のトレースポイントの例を次に示します。

[[Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=47 uniqueID=45857 name=Organizational Assessment]], [Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=49 uniqueID=45859 name=Document Deliverables]], [Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=56 uniqueID=45866 name=Infrastructure Deliverables]], [Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=58 uniqueID=45868 name=IT Deliverables]], [Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=60 uniqueID=45870 name=Organizational Deliverables]]]

ご協力ありがとうございました。

4

3 に答える 3

4

C#3.5以降では、これらのJavaリストの優れたタイプセーフな列挙子を提供する拡張メソッドを作成することは、yieldキーワードのおかげで非常に簡単です。あなたはそのようにそれを行うことができます:

public static class JavaExtensions
{

        public static IEnumerable<T> toIEnumerable<T>(this java.util.List list)
        {
            if (list != null)
            {
                java.util.Iterator itr = list.iterator();

                while (itr.hasNext())
                {
                    yield return (T)itr.next();
                }
            }
        }

}

この拡張機能を含む名前空間がコードに表示されている限り、次のようにして先行操作を操作できます。

foreach (var relation in task.getPredecessors().toIEnumerable<Relation>())
{
   Task sourceTask = relation.getSourceTask();
   //etc.
}

残念ながら、VB.Netを使用しています。私はあなたと同じ船に乗っており、VB ASP.NETプロジェクトで参照する別のC#クラスライブラリを作成することになりました。そうすれば、プロジェクト全体を変換しなくても、IKVM Javaスタイルのオブジェクトを処理する際にC#構文の利点を得ることができます。

それを望まない場合は、オンラインコードコンバーターを使用して、Jonのコード(C#のみの機能を使用しない)をVBに変更し、プロジェクトに含めることができます。

上記の場合の主なことは、デバッガーに表示される文字列表現を操作する必要がないことです(これは、そのリストのRelationオブジェクトでtoString()を呼び出すだけです)。このgetPredecessors()関数は、Relationオブジェクトのリストを返します。

Dim predecessorList as java.util.List = task.getPredecessors()
Dim iter as java.util.Iterator = predecessorList.iterator()

Do While iter.hasNext()
    Dim curRelation as Relation = iter.next()

    'gets the left side of the relationship (the task you are dealing with)
    Dim sourceTask as Task = curRelation.getSourceTask()

    'gets the task that is the predecessor to the  'source' task
    Dim targetTask as Task = curRelation.getTargetTask()

    'from here you can call methods on the Task objects to get their other properties
    'like name and id
Loop
于 2012-03-19T18:07:45.903 に答える
1

IKVMによって生成された.Netアセンブリを使用する場合、Javaコレクションを操作する方法は2つあります。1つ目は、Javaの方法で処理を行い、イテレーターを使用することです。

java.util.List predecessors = task.getPredecessors();
java.util.Iterator iter = predecessors.iterator();
while (iter.hasNext())
{
   Relation rel = (Relation)iter.next();
   System.Console.WriteLine(rel);
}

もう1つの方法は、これを非表示にするための小さな「ユーザビリティ」コードを追加することです。

foreach(Relation rel in ToEnumerable(task.getPredecessors()))
{
   System.Console.WriteLine(rel);
}

これを行うために、「ToEnumerable」メソッドを作成しました。

private static EnumerableCollection ToEnumerable(Collection javaCollection)
{
   return new EnumerableCollection(javaCollection);
}

EnumerableCollectionクラスは、イテレータの使用を隠します。

class EnumerableCollection
{
    public EnumerableCollection(Collection collection)
    {
        m_collection = collection;
    }

    public IEnumerator GetEnumerator()
    {
        return new Enumerator(m_collection);
    }

    private Collection m_collection;
}


public struct Enumerator : IEnumerator
{
    public Enumerator(Collection collection)
    {
        m_collection = collection;
        m_iterator = m_collection.iterator();
    }

    public object Current
    {
        get
        {
            return m_iterator.next();
        }
    }

    public bool MoveNext()
    {
        return m_iterator.hasNext();
    }

    public void Reset()
    {
        m_iterator = m_collection.iterator();
    }

    private Collection m_collection;
    private Iterator m_iterator;
}

これを行うためのより興味深い方法は、拡張メソッドを使用してこの機能をJavaコレクションクラスに追加することです。これにより、コードが少し整理されます。次のリリースでは、MPXJの一部として、便利な拡張メソッドを含むアセンブリを出荷する予定です。

于 2012-03-19T13:34:13.680 に答える
0

tostringを使用してこれを完了し、次に右と左を使用して必要なものを切り取ることができました。これが以前に削除された理由がわかりません。

于 2012-03-22T17:12:07.667 に答える