I am building a console application that uses a MSH style CmdParser (
http://channel9.msdn.com/playground/Sandbox/52952/) to read input parameters
and reflect match them up against a data class defining their meaning.
In the attempt to use RSpec & IronRuby to test my .net application, the code
blows up on a call to the below method:
private static Parameter[] GetPublicReadWriteCmdMembers(object instance)
{
if ( instance == null )
throw new ArgumentNullException("instance");
ArrayList al = new ArrayList();
Type type = instance.GetType();
ArrayList members = new ArrayList();
members.AddRange(type.GetProperties());
members.AddRange(type.GetFields());
if ( members.Count == 0 )
throw new ArgumentException("No public members in
type.");
foreach(MemberInfo mi in members)
{
// Only add members that have ParameterBaseAttribute(s).
if ( ! mi.IsDefined(typeof(ParameterBaseAttribute), true) )
continue;
switch(mi.MemberType)
{
case MemberTypes.Property:
PropertyInfo pi = (PropertyInfo)mi;
if ( ! (pi.PropertyType.IsPublic && pi.CanRead
&&
pi.CanWrite) )
throw new ArgumentException("All CMD members
must be public readable and writeable.");
// Loop here on members if parameterAttributes.
object[] pArray
pi.GetCustomAttributes(typeof(ParameterAttribute), true);
if ( pArray != null && pArray.Length > 0 )
{
foreach(ParameterAttribute pa in pArray)
{
Parameter p Parameter.CreateParameter(instance,
mi, pa);
al.Add(p);
}
}
else
{
// Use default ParameterAttribute.
ParameterAttribute pa = new
ParameterAttribute();
Parameter p Parameter.CreateParameter(instance, mi,
pa);
al.Add(p);
}
break;
case MemberTypes.Field:
FieldInfo fi = (FieldInfo)mi;
if ( ! fi.FieldType.IsPublic )
throw new ArgumentException("All Cmd members
must be public");
object[] pArray2
fi.GetCustomAttributes(typeof(ParameterAttribute), true);
if ( pArray2 != null && pArray2.Length > 0 )
{
foreach(ParameterAttribute pa in pArray2)
{
Parameter p Parameter.CreateParameter(instance,
mi, pa);
al.Add(p);
}
}
else
{
// Use default ParameterAttribute.
ParameterAttribute pa = new
ParameterAttribute();
Parameter p Parameter.CreateParameter(instance, mi,
pa);
al.Add(p);
}
break;
default:
break;
}
}
return (Parameter[])al.ToArray(typeof(Parameter));
}
Specifically, the exception thrown is on the call: if ( !
mi.IsDefined(typeof(ParameterBaseAttribute), true) ) which is using
reflection to figure out if the input parameter is defined in my data class
(as best I can tell).
The exception is:
Could not load file or assembly ''CmdParser, Version=1.5.0.0,
Culture=neutral, PublicKeyToken=null'' or one of its dependencies. The
system
cannot find the file specified.
So. I am stuck at a bit of an impasse. I tried requiring the CmdParser
assembly in the way it claims above and it indeed blows up. However, if I
call require.the dll itself and do some testing in IR I can repeat the
exception during the reflection call.
Any ideas for how I might get through this?
-Andrew
--
When I wrote this, only God and I understood what I was doing. Now, God
only knows - Karl Weierstrass
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/ironruby-core/attachments/20100109/e8f7b63e/attachment.html>
you can try copying your cmdparser.dll to the bin folder where ir.exe lives. That might work because I assume you''ve got it in the bin of your application already. Don''t shoot me if it doesn''t. I had to do this when I wanted to use particular control sets in WPF but that was a long time ago (>1yr) so things might be different now. --- Met vriendelijke groeten - Best regards - Salutations Ivan Porto Carrero Blog: http://flanders.co.nz Twitter: http://twitter.com/casualjim Author of IronRuby in Action (http://manning.com/carrero) On Sun, Jan 10, 2010 at 12:24 AM, andrew Wilson <a.wilson82 at gmail.com>wrote:> I am building a console application that uses a MSH style CmdParser ( > http://channel9.msdn.com/playground/Sandbox/52952/) to read input > parameters and reflect match them up against a data class defining their > meaning. > > In the attempt to use RSpec & IronRuby to test my .net application, the > code blows up on a call to the below method: > > private static Parameter[] GetPublicReadWriteCmdMembers(object instance) > { > if ( instance == null ) > throw new ArgumentNullException("instance"); > > ArrayList al = new ArrayList(); > Type type = instance.GetType(); > ArrayList members = new ArrayList(); > members.AddRange(type.GetProperties()); > members.AddRange(type.GetFields()); > > if ( members.Count == 0 ) > throw new ArgumentException("No public members in type."); > > foreach(MemberInfo mi in members) > { > // Only add members that have ParameterBaseAttribute(s). > if ( ! mi.IsDefined(typeof(ParameterBaseAttribute), true) ) > continue; > > switch(mi.MemberType) > { > case MemberTypes.Property: > PropertyInfo pi = (PropertyInfo)mi; > if ( ! (pi.PropertyType.IsPublic && pi.CanRead && > pi.CanWrite) ) > throw new ArgumentException("All CMD members > must be public readable and writeable."); > > // Loop here on members if parameterAttributes. > object[] pArray > pi.GetCustomAttributes(typeof(ParameterAttribute), true); > if ( pArray != null && pArray.Length > 0 ) > { > foreach(ParameterAttribute pa in pArray) > { > Parameter p > Parameter.CreateParameter(instance, mi, pa); > al.Add(p); > } > } > else > { > // Use default ParameterAttribute. > ParameterAttribute pa = new > ParameterAttribute(); > Parameter p > Parameter.CreateParameter(instance, mi, pa); > al.Add(p); > } > break; > case MemberTypes.Field: > FieldInfo fi = (FieldInfo)mi; > if ( ! fi.FieldType.IsPublic ) > throw new ArgumentException("All Cmd members > must be public"); > > object[] pArray2 > fi.GetCustomAttributes(typeof(ParameterAttribute), true); > if ( pArray2 != null && pArray2.Length > 0 ) > { > foreach(ParameterAttribute pa in pArray2) > { > Parameter p > Parameter.CreateParameter(instance, mi, pa); > al.Add(p); > } > } > else > { > // Use default ParameterAttribute. > ParameterAttribute pa = new > ParameterAttribute(); > Parameter p > Parameter.CreateParameter(instance, mi, pa); > al.Add(p); > } > > break; > default: > break; > } > } > return (Parameter[])al.ToArray(typeof(Parameter)); > } > > Specifically, the exception thrown is on the call: if ( ! > mi.IsDefined(typeof(ParameterBaseAttribute), true) ) which is using > reflection to figure out if the input parameter is defined in my data class > (as best I can tell). > > The exception is: > > Could not load file or assembly ''CmdParser, Version=1.5.0.0, > Culture=neutral, PublicKeyToken=null'' or one of its dependencies. The system > cannot find the file specified. > > So. I am stuck at a bit of an impasse. I tried requiring the CmdParser > assembly in the way it claims above and it indeed blows up. However, if I > call require.the dll itself and do some testing in IR I can repeat the > exception during the reflection call. > > Any ideas for how I might get through this? > > -Andrew > > -- > When I wrote this, only God and I understood what I was doing. Now, God > only knows - Karl Weierstrass > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20100111/b8e1dd26/attachment.html>
That worked! That would be a fun bug to track down. :) I was worried that it was reflection against an attribute, because the documentation on the IR site seems to suggest that attributes still aren''t supported in .net API interop. ( http://ironruby.net/Documentation/.NET/API_Guidelines) Thanks again! -A -- When I wrote this, only God and I understood what I was doing. Now, God only knows - Karl Weierstrass -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20100111/1a5e7aa2/attachment.html>
That''s a bit ambiguous. If your C# library uses attributes I don''t see why you wouldn''t be able to reflect over those types with IronRuby and execute the attribute. But using attributes in Ruby that is a totally different story. Luckily ruby has a different idea about classes and you can use class methods to reach a similar effect. For example ASP.NET MVC uses attributes to do its job, but IronRubyMvc has no need for them because it uses those class methods. http://github.com/casualjim/ironrubymvc/blob/master/IronRubyMvc/Controllers/controller.rb#L173 --- Met vriendelijke groeten - Best regards - Salutations Ivan Porto Carrero Blog: http://flanders.co.nz Twitter: http://twitter.com/casualjim Author of IronRuby in Action (http://manning.com/carrero) On Mon, Jan 11, 2010 at 3:31 PM, andrew Wilson <a.wilson82 at gmail.com> wrote:> That worked! That would be a fun bug to track down. :) > > I was worried that it was reflection against an attribute, because the > documentation on the IR site seems to suggest that attributes still aren''t > supported in .net API interop. ( > http://ironruby.net/Documentation/.NET/API_Guidelines) > > Thanks again! > -A > > > -- > When I wrote this, only God and I understood what I was doing. Now, God > only knows - Karl Weierstrass > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20100111/6dc0c00f/attachment.html>