2010-07-28 2 views
3

How do I return an array of strings from an ActiveX object to JScript과 유사하지만 C#.배열을 C# COM 개체에서 JavaScript로 전달 하시겠습니까?

문자열 배열을 자바 스크립트로 전달하는 COM 컨트롤이 있습니다. 그것은 javascript 캔트 내가 다시 전달하고 자바 스크립트에서 배열은 항상 정의되지 않은 이해할 것 같다.

자바 스크립트 :

public object[] getAllFriendlyNames() 
    { 
     if (!keystoreInitialized) 
      throw new Exception("Key store has not been initialized"); 

     X509Certificate2Collection allCerts = certificateStore.Certificates; 

     int storeSize = allCerts.Count; 

     if (storeSize == 0) 
      throw new Exception("Empty Key Store, could have opened using the wrong keystore name."); 

     object[] friendlyNames = new object[storeSize]; 

     for (int i = 0; i < storeSize; i++) 
     { 
      string friendlyName = allCerts[i].FriendlyName; 

      if (friendlyName == "") 
       friendlyName = allCerts[i].Subject; 

      friendlyNames[i] = (object) friendlyName; 
     } 

     return friendlyNames; 
    } 

나는 아무 소용 객체 배열과 문자열 배열을 모두 반환 시도했다 : 출력 '정의되지 않은'fNames[0];

C 번호가

try 
{ 
    keystore.openKeyStore("MY", true, false); 
    var fNames = new Array(); 
    fNames = keystore.getAllFriendlyNames(); 
    document.getElementById('par').innerHTML = fNames[0]; 
} 
catch(err) 
{ 
    document.getElementById('err').innerHTML = err.description; 
} 

.

답변

2

데이터를 json으로 직렬화하고 클라이언트에서 직렬화를 시도 할 수 있습니다. jQuery는 json 함수를 내장하고 있습니다. 더 복잡한 객체를 사용하여이 작업을 수행했지만 문자열 배열은 사용하지 않았습니다. 다만 쉽게 수행 할 수 있습니다.

2

당신은 당신의 액티브 X 방식에서 직접 자바 스크립트 배열을 보낼 수 있습니다, 함수는 다음과 같습니다

public ObjectArray getAllFriendlyNames() 
{ 
    //.... the same ...... 
    return Microsoft.JScript.GlobalObject.Array.ConstructArray(friendlyNames); 
} 

프로젝트에 Microsoft.JScript 참조를 추가.

관련 문제