File size: 2,076 Bytes
8739cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
if dotNetInfo==nil then
  dotNetInfo={}
end

function dotNetInfo.createInstanceOfClassDialog(Class)
  if Class==nil then return nil,'No class provided' end

  --printf("Creating instance of "..Class.Name)

  if (Class.Image.Domain.Control~=0) then
    return nil,'Only mono/il2pp is supported' --not currently supported
  end

  if Class.Methods==nil then
    return nil,'Class.Methods is not set'
  end


  --find .ctor's
  local ctorlist={}
  for i=1,#Class.Methods do
    if Class.Methods[i].Name=='.ctor' then
      table.insert(ctorlist, Class.Methods[i])
    end
  end

  if #ctorlist==0 then return nil,'No .ctor' end

  local instanceAddress=mono_object_new(Class.Handle)

  if instanceAddress==nil or instanceAddress==0 then
    return nil,'Failure creating instance of this class'
  end

 -- printf("New instanceAddress=%x", instanceAddress)


  local Method
  if #ctorlist>1 then
    --ask which one to use
    local sl=createStringList()
    for i=1,#ctorlist do
      sl.add(ctorlist[i].ReturnType..' '..ctorlist[i].Parameters)
    end

    local i,r=showSelectionList(Class.FullName..' .ctors', "Select the constructor you'd like to use", sl, false)
    if i==-1 then return nil end

    Method=ctorlist[i+1]

    sl.destroy()
  else
    Method=ctorlist[1]
  end

  if Method==nil then
    return nil,'No method selected'
  end


  if #Method.Parameters==0 then
    --no parameters needed, call the constructor directly
    mono_invoke_method(Method.Class.Image.Domain.Handle, Method.Handle, instanceAddress,{})
    if result==nil then
      messageDialog('Failure to call constructor:'.._secondary,mtWarning)
    end
  else
    --constructor has parameters, ask to fill them in
    --printf("Calling dotNetInfo.SpawnInvokeMethodDialog with address %x", instanceAddress)

    local r,err=dotNetInfo.SpawnInvokeMethodDialog(Method, instanceAddress)
    if r==nil and err then
      messageDialog('Failure to call constructor:'..err,mtWarning)
    end
  end

  return instanceAddress
end