|
In the following code snippet myMath class has five Attributes, the problem which I am facing is that on the console I don't get the Order of the Attributes , right. The attribute with bugID = 1 shows on the top, and strangely it is followed by the fifth attribute bugID = 5, and then 2,3,4.
When I remove the fifth Attribute this is the order of the Attributes 2,1,3,4.
When there are three attributes the behavior is normal as in 1,2,3.
When there are two attributes again the behavior changes to 2,1.
Can Anyone pl. figure out the problem for me
using System.Collections.Generic;
using System;
using System.Linq;
using System.Text;
using System.Reflection;
namespace UsingAttributes
{
[AttributeUsage(AttributeTargets.All,AllowMultiple = true )]
class BugFixAttribute : Attribute
{
private int bugID;
private string coder;
private string date;
private string comment;
public BugFixAttribute(int bugID, string coder, string date)
{
this.bugID = bugID;
this.coder = coder;
this.date = date;
}
public string Comment
{
get
{
return comment;
}
set
{
comment = value;
}
}
public int BugID
{
get
{
return bugID;
}
}
public string Coder
{
get
{
return coder;
}
}
public string Date
{
get
{
return date;
}
}
}
[BugFix(001, "sh", "08/11/2007", Comment = "one")]
[BugFix(002, "sh", "08/11/2007", Comment = "two")]
[BugFix(003, "sh", "08/11/2007", Comment = "three")]
[BugFix(004, "sh", "08/11/2007", Comment = "four")]
[BugFix(005, "sh", "08/11/2007", Comment = "five")]
public class myMath
{
public double DoFunc1(double param1)
{
return param1 + DoFunc2(param1);
}
public double DoFunc2(double param1)
{
return param1 / 3;
}
}
class Program
{
static void Main(string[] args)
{
myMath mm = new myMath();
Console.WriteLine("Result for DoFunc(7) = {0}", mm.DoFunc1(7));
MemberInfo inf = typeof(myMath);
object[] attributes = inf.GetCustomAttributes(typeof(BugFixAttribute), false);
foreach (object attribute in attributes)
{
BugFixAttribute b = attribute as BugFixAttribute;
Console.WriteLine("\nBugID: {0}", b.BugID);
Console.WriteLine("Programmer: {0}", b.Coder);
Console.WriteLine("Date : {0}", b.Date);
Console.WriteLine("Comment: {0}", b.Comment);
}
Console.ReadKey();
}
}
}
Thanx
|
|
|
|
|
AFAIK the call to GetCustomAttributes does not do so in any recognisable order. I have faced a similar issue in the past where the order of arributes was important so I created a custom static method to get my attributes in an ordered fashion.
In your case this could well use the BugID property to order on.
|
|
|
|
|
Jamies entirely right in what he said about retrieving the attributes by order. What you could do is implement a delegate to sort them into order in your reading code.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Or make an attribute collection?
[BugFixAttributeCollection(
new BugFix(001, "sh", "08/11/2007", Comment = "one") ,
new BugFix(002, "sh", "08/11/2007", Comment = "two") ,
new BugFix(003, "sh", "08/11/2007", Comment = "three") ,
new BugFix(004, "sh", "08/11/2007", Comment = "four") ,
new BugFix(005, "sh", "08/11/2007", Comment = "five")
)]
I'd better try it to see whether or not it works...
P.S. Use an ISO 8601 compliant date format.
-- modified at 21:50 Thursday 8th November, 2007
Well it doesn't work as shown above, but I'm getting close...
|
|
|
|
|
I don't suppose you'd want something like this...
[GroupOfAttribute(
"<OneAttribute Num=\"001\" Text=\"sh\" When=\"2007-11-08\" />"
,
"<OneAttribute Num=\"002\" Text=\"sh\" When=\"2007-11-08\" />"
,
"<OneAttribute Num=\"003\" Text=\"sh\" When=\"2007-11-08\" />"
)]
Ugly ugly ugly... error prone and not very typesafe... but it works!
|
|
|
|
|
Hi
I saw from your example that you are using framework 3.0 or above
Try using the follwoing code
List<BugFixAttribute> lst = new List<BugFixAttribute>((IEnumerable<BugFixAttribute> )inf.GetCustomAttributes(typeof(BugFixAttribute), false));
lst.Sort((a, b) => a.BugID < b.BugID ? -1 : 1);
|
|
|
|
|
hi
i use Sql server 2005 reporting Service in my app in local processing mode, in report designer i add a textBox and i want send value from my csharp code to textBox of report, i use this code :
try
{
this.v_customerOrderCountTableAdapter.Fill(this.NorthwindDataSet.v_customerOrderCount);
Microsoft.Reporting.WinForms.ReportParameter rp = new Microsoft.Reporting.WinForms.ReportParameter("textbox5", "Param From Code");
this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter[] { rp });
this.reportViewer1.RefreshReport();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
but the following error shown me :
An error occurred during local report processing.
where does my problem and how to send parameters deom my code to report ?
thanks
|
|
|
|
|
Did you add the parameter to the report itself? If the report has parameter called paramater1 and you want to set its value to myvalue then you should do it like this:
<br />
Microsoft.Reporting.WinForms.ReportParameter rp = new Microsoft.Reporting.WinForms.ReportParameter("paramater1", "myvalue");<br />
this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter[] { rp });<br />
|
|
|
|
|
i add in a textBox named 'textBox5' to my report in report designer and wrote your code (that like my code), but the above error has shown me.
|
|
|
|
|
It seems that you haven't added the parameter to the report itself
|
|
|
|
|
Hello all,
I have a curious problem relating to XML deserialisation where the deserialiser allows malformed XML to pass through without issue. This needs some further information, so without further ado:
I have the following simple schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="TestSchema">
<xs:complexType>
<xs:sequence>
<xs:element name="Widgets" type="xs:string" nillable="false"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
From this I have created .NET 2 c# classes using the XSD tool, this result in the following class
using System.Xml.Serialization;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class TestSchema {
private string widgetsField;
public string Widgets {
get {
return this.widgetsField;
}
set {
this.widgetsField = value;
}
}
}
This is then added to a simple test project that looks like this:
static void Main(string[] args)
{
string rawXml = "<TestSchema>";
rawXml += "<Widgets>A value for widgets</Widgets>";
rawXml += "<LaLa>An element that doesnt exist in the schema</LaLa>";
rawXml += "</TestSchema>";
byte[] data = System.Text.Encoding.ASCII.GetBytes(rawXml);
MemoryStream fs = new MemoryStream(data);
TextReader reader = new StreamReader(fs);
XmlSerializer ser = new XmlSerializer(typeof(TestSchema));
TestSchema obj = (TestSchema)ser.Deserialize(reader);
reader.Close();
Console.WriteLine(obj.GetHashCode());
Console.ReadLine();
}
Now all this works, however I wouldn't expect it to! The more eagled eyed will have noticed the following line in the code above:
rawXml += "<LaLa>An element that doesnt exist in the schema</LaLa>";
When the deserialiser is processing the XML string, it seems that any elements that are not in the C# schema class are simply ignored. So the first question is, does anybody know of a way to run the deserialiser in "strict" mode, so that if an unknown element is present then an exception occurs.
When testing different scenarios I also discovered that I could remove a nillable false element and still deserialise, which resulted in the object value being, obviously, null. For example :
string rawXml = "<TestSchema>";
rawXml += "</TestSchema>";
or
string rawXml = "<TestSchema>";
rawXml += "<LaLa>Hold on whats this an element that doesnt exist in the schema</LaLa>";
rawXml += "</TestSchema>";
So the second questions is, is there any way to run the deserialiser in "strict" mode where if a mandatory/nillable false element is not found and exception occurs.
An immediately obvious solution to this problem is to pass the XML through a validator first and then perform the deserialisation. While this will work, in the real world the application is processing alot of XML data and having to validate every one will add to processing time. I would prefer to only validate if a deserialisation exception occurs.
-- modified at 10:31 Thursday 8th November, 2007
|
|
|
|
|
It would seem I have found the solution, a tweak to the code above and malformed XML is no longer allowed:
static void Main(string[] args)
{
string rawXml = "<TestSchema>";
rawXml += "<Widgets>A value for widgets</Widgets>";
rawXml += "<LaLa>Hold on whats this an element that doesnt exist in the schema</LaLa>";
rawXml += "</TestSchema>";
XmlSerializer s = new XmlSerializer(typeof(TestSchema));
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(rawXml);
MemoryStream ms = new MemoryStream(buffer);
XmlReader reader = new XmlTextReader(ms);
XmlDeserializationEvents deserializationEvents = new XmlDeserializationEvents();
deserializationEvents.OnUnknownAttribute = new XmlAttributeEventHandler(UnknownAttributeEventHandler);
deserializationEvents.OnUnknownElement = new XmlElementEventHandler(UnknownElementEventHandler);
deserializationEvents.OnUnknownNode = new XmlNodeEventHandler(UnknownNodeEventHandler);
deserializationEvents.OnUnreferencedObject = new UnreferencedObjectEventHandler(UnreferencedObjEventHandler);
TestSchema obj = (TestSchema)s.Deserialize(reader, deserializationEvents);
reader.Close();
Console.WriteLine(obj.GetHashCode());
Console.ReadLine();
}
private static void UnknownAttributeEventHandler(object sender, XmlAttributeEventArgs e)
{
}
private static void UnknownElementEventHandler(object sender, XmlElementEventArgs e)
{
}
private static void UnknownNodeEventHandler(object sender, XmlNodeEventArgs e)
{
}
private static void UnreferencedObjEventHandler(object sender, UnreferencedObjectEventArgs e)
{
}
As you can see in the code above, the XmlSerializer.Deserialize can also take an instance of XmlDeserializationEvents. If this is setup and passed in these events are created and from there I can handle the error condition.
|
|
|
|
|
Good to know... thanks for posting that!
|
|
|
|
|
Since posting the original solution I have found that you can also set these event directly on the XmlSerializer
XmlSerializer xmlSerializer = new XmlSerializer(objectType);
xmlSerializer.UnknownAttribute += new XmlAttributeEventHandler(xmlSerializer_UnknownAttribute);
xmlSerializer.UnknownElement += new XmlElementEventHandler(xmlSerializer_UnknownElement);
xmlSerializer.UnknownNode += new XmlNodeEventHandler(xmlSerializer_UnknownNode);
xmlSerializer.UnreferencedObject += new UnreferencedObjectEventHandler(xmlSerializer_UnreferencedObject);
From a functionality point of view I don't suppose it makes any difference at all, however IMHO this method is neater.
|
|
|
|
|
hello,
un have 2 application and i send messages between them using named pipes technologie;
i send 2 messages , the first for showing the application and the second for hiding it;
the probleme is before sending the messages i have to know the state of application (show/hide).
how can i know the state of my second application?
|
|
|
|
|
You'll need a method that queries the state of the second application.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
whitch method do you talking about? i can figure out
please help
|
|
|
|
|
A method which you have to write in order to figure out the state of the application !
AliAmjad(MCP)
First make it Run THEN make it Run Fast.
|
|
|
|
|
i m so sorry, but i dont understand what do you mean,
i want to know can i know the state of a dialog or application (hide/show) ?
if yes , how?
thanks
|
|
|
|
|
This is easy. Create a method that checks if the form is visible or not.
(Hint: use the Visible property)
Kristian Sixhoej
"Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"
|
|
|
|
|
Check to see if the form is visible.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
i want to know if is visible from another application not the same application
i hope that now that s is more clear
|
|
|
|
|
Morad SAJID wrote: i want to know if is visible from another application not the same application
i hope that now that s is more clear
How can I put this simply? If you want to know if application B is visible or not, then application A is going to have to query it and ask it if it is visible or not. Hence, application B will have to provide this information. That's what we've been telling you all along. There's no easy way for the first application to know this - it's the responsibility of the second app to tell it.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
yes thank you
i have an idéa but i don't know if it will be works;
i ll change the process name of the application A, the application B will looking for the first name of application A , when it will not find it it will know that my application is hided,
but how to change the processname of my application;
thakks again
|
|
|
|
|
I would have the application that shows/hides create a named Mutex when it is visible. It can release the Mutex when it gets hidden.
The calling app can try and create the same named Mutex and if it fails, it knows the application is visible. If it succeeds, it releases the Mutex and knows that the application is hidden.
|
|
|
|
|