Search Results

Search found 1919 results on 77 pages for 'typeof'.

Page 34/77 | < Previous Page | 30 31 32 33 34 35 36 37 38 39 40 41  | Next Page >

  • Dynamically created jagged "rectangle" array

    - by gagar1n
    In my project I have a lot of code like this: int[][] a = new int[firstDimension][]; for (int i=0; i<firstDimension; i++) { a[i] = new int[secondDimension]; } Types of elements are different. Is there any way of writing a method like createArray(typeof(int), firstDimension, secondDimension); and getting new int[firstDimension][secondDimension]? Once again, type of elements is known only at runtime.

    Read the article

  • Dictionary w/ null key?

    - by Ralph
    Firstly, why doesn't Dictionary<TKey, TValue> support a single null key? Secondly, is there an existing dictionary-like collection that does? I want to store an "empty" or "missing" or "default" System.Type, thought null would work well for this. More specifically, I've written this class: class Switch { private Dictionary<Type, Action<object>> _dict; public Switch(params KeyValuePair<Type, Action<object>>[] cases) { _dict = new Dictionary<Type, Action<object>>(cases.Length); foreach (var entry in cases) _dict.Add(entry.Key, entry.Value); } public void Execute(object obj) { var type = obj.GetType(); if (_dict.ContainsKey(type)) _dict[type](obj); } public static void Execute(object obj, params KeyValuePair<Type, Action<object>>[] cases) { var type = obj.GetType(); foreach (var entry in cases) { if (entry.Key == null || type.IsAssignableFrom(entry.Key)) { entry.Value(obj); break; } } } public static KeyValuePair<Type, Action<object>> Case<T>(Action action) { return new KeyValuePair<Type, Action<object>>(typeof(T), x => action()); } public static KeyValuePair<Type, Action<object>> Case<T>(Action<T> action) { return new KeyValuePair<Type, Action<object>>(typeof(T), x => action((T)x)); } public static KeyValuePair<Type, Action<object>> Default(Action action) { return new KeyValuePair<Type, Action<object>>(null, x => action()); } } For switching on types. There are two ways to use it: Statically. Just call Switch.Execute(yourObject, Switch.Case<YourType>(x => x.Action())) Precompiled. Create a switch, and then use it later with switchInstance.Execute(yourObject) Works great except when you try to add a default case to the "precompiled" version (null argument exception).

    Read the article

  • How could I know if an object is derived from a specific generic class?

    - by Edison Chuang
    Suppose that I have an object then how could I know if the object is derived from a specific generic class. For example: public class GenericClass<T> { } public bool IsDeriveFrom(object o) { return o.GetType().IsSubclassOf(typeof(GenericClass)); //will throw exception here } please notice that the code above will throw an exception. The type of the generic class cannot be retrieved directly because there is no type for a generic class without a type parameter provided.

    Read the article

  • Java generics - getting the type..

    - by peter
    Hi! I'm a c# guy giving Java a try .. so how would I do the following in java. in C# public T create_an_instance_of<T>(){ T instance = default (T); // here's usually some factory to create the implementation instance = some_factory.build<T>(); // or even.. instance = some_factory.build(typeOf(T) ); return instance; }

    Read the article

  • asp.net mvc2 validate type double

    - by ile
    [MetadataType(typeof(Deal_Validation))] public partial class Deal { } public class Deal_Validation { [Required] public string Title { get; set; } public double? EstValue { set; get; } } How to validate EstValue (check if it is of type double?) Thanks

    Read the article

  • Invoke target throwing invalid cross-thread operate exception

    - by sqwerty
    MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic); mi.Invoke(notify, null); This throws the following exception: {"Exception has been thrown by the target of an invocation."} With the following inner exception: "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on." If I comment out a line of code that sets the images for the context menu entries then it stops throwing the exception. Any ideas?

    Read the article

  • restricting the property type of a custom attribute

    - by Guy
    Does anyone knows if it is possible to define/declare on your own custom attribute a restriction to the field type it may apply on? There are a flags that do restrict the usage of the attribute: [AttributeUsage( AttributeTargets.Property, AllowMultiple = false)] Im looking for something like: UseOnlyOnType = typeof(string) Any ideas?

    Read the article

  • How to access hosted WCF service methods?

    - by Qutbuddin Kamaal
    Hi, I created a WCF service name 'WasSettingsService' have method 'GetWASSettings' and Hosted this service like this: ServiceHost myServiceHost = new ServiceHost(typeof(LocalMachineSettingsService.WasSettingsService)); myServiceHost.Open(); Now How can I access 'GetWASSettings' Thanks in advance will really appreciate this..

    Read the article

  • Why is my (Type).GetFields(BindingFlags.Instance | BindingFlags.Public) not working?

    - by granadaCoder
    My code can see the NonPublic members, but not the Public ones. (???) Full sample code below. FieldInfo[] publicFieldInfos = t.GetFields(BindingFlags.Instance | BindingFlags.Public); is returning nothing. Note, I'm trying to get at the properties on the abstract class as well as the 1 concrete class. (And read the attributes as well). I'm going bonkers on this one....the msdn example works with the 2 flags (BindingFlags.Instance | BindingFlags.Public).....but my mini inheritance example below is not. THANKS in advance. /////////////START CODE private void RunTest1() { try { textBox1.Text = string.Empty; Type t = typeof(MyInheritedClass); //Look at the BindingFlags *** NonPublic *** int fieldCount = 0; while (null != t) { fieldCount += t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Length; FieldInfo[] nonPublicFieldInfos = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (FieldInfo field in nonPublicFieldInfos) { if (null != field) { Console.WriteLine(field.Name); } } t = t.BaseType; } Console.WriteLine("\n\r------------------\n\r"); //Look at the BindingFlags *** Public *** t = typeof(MyInheritedClass); FieldInfo[] publicFieldInfos = t.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (FieldInfo field in publicFieldInfos) { if (null != field) { Console.WriteLine(field.Name); object[] attributes = field.GetCustomAttributes(t, true); if (attributes != null && attributes.Length > 0) { foreach (Attribute att in attributes) { Console.WriteLine(att.GetType().Name); } } } } } catch (Exception ex) { ReportException(ex); } } private void ReportException(Exception ex) { Exception innerException = ex; while (innerException != null) { Console.WriteLine(innerException.Message + System.Environment.NewLine + innerException.StackTrace + System.Environment.NewLine + System.Environment.NewLine); innerException = innerException.InnerException; } } public abstract class MySuperType { public MySuperType(string st) { this.STString = st; } public string STString { get; set; } public abstract string MyAbstractString {get;set;} } public class MyInheritedClass : MySuperType { public MyInheritedClass(string ic) : base(ic) { this.ICString = ic; } [Description("This is an important property"),Category("HowImportant")] public string ICString { get; set; } private string _oldSchoolPropertyString = string.Empty; public string OldSchoolPropertyString { get { return _oldSchoolPropertyString; } set { _oldSchoolPropertyString = value; } } [Description("This is a not so importarnt property"), Category("HowImportant")] public override string MyAbstractString { get; set; } }

    Read the article

  • How to convert string to any type

    - by DJPB
    Hi there I want to convert a string to a generic type I have this: string inputValue = myTxtBox.Text; PropertyInfo propInfo = typeof(MyClass).GetProperty(myPropertyName); Type propType = propInfo.PropertyType; object propValue = ????? I want to convert 'inputString' to the type of that property, to check if it's compatible how can I do that? tks

    Read the article

  • Jump to the start of next statement

    - by Borek
    Say that I'm writing a test and my caret is here (indicated by "|"): [ExpectedException(typeof(InvalidOperationException|))] In Visual Studio 2010, I'd like to press some shortcut that would take me to the next line so that I could start typing "public void Etc()". Right now, I need to press Right arrow Right arrow Right arrow Enter Which is too many key presses IMO. Is there a VS2010 / ReSharper shortcut to aid this operation?

    Read the article

  • Nhiberate, multiple tables, same class

    - by jeff
    It's been asked a million times, its like this. Say Invoice is the base class and InvoiceHistory is the class that simply inherits from Invoice. When I do something like invoiceList = session.CreateCriteria(typeof(Invoice)).List(); I get everything from Invoice (that I want, plus everything from InvoiceHistory). Do I need to have an InvoiceBase and create derived versions for Invoice and InvoiceHistory?

    Read the article

  • Binding one dependency property to another

    - by Gregory Dodd
    I have a custom Tab Control that I have created, but I am having an issue. I have an Editable TextBox as part of the custom TabControl View. <Controls:EditableTextControl x:Name="PageTypeName" Style="{StaticResource ResourceKey={x:Type Controls:EditableTextControl}}" Grid.Row="0" TabIndex="0" Uid="0" AutomationProperties.AutomationId="PageTypeNameTextBox" AutomationProperties.Name="PageTypeName" Visibility="{Binding ElementName=PageTabControl,Path=ShowPageType}"> <Controls:EditableTextControl.ContextMenu> <ContextMenu x:Name="TabContextMenu"> <MenuItem Header="Rename Page Type" Command="{Binding Path=PlacementTarget.EnterEditMode, RelativeSource={RelativeSource AncestorType=ContextMenu}}" AutomationProperties.AutomationId="RenamePageTypeMenuItem" AutomationProperties.Name="RenamePageType"/> <MenuItem Header="Delete Page Type" Command="{Binding Path=PageTypeDeletedCommand}" AutomationProperties.AutomationId="DeletePageTypeMenuItem" AutomationProperties.Name="DeletePageType"/> </ContextMenu> </Controls:EditableTextControl.ContextMenu> <Controls:EditableTextControl.Content> <!--<Binding Path="CurrentPageTypeViewModel.Name" Mode="TwoWay"/>--> <Binding ElementName="PageTabControl" Path="CurrentPageTypeName" Mode ="TwoWay"/> </Controls:EditableTextControl.Content> </Controls:EditableTextControl> In the Content section I am binding to a Dependency Prop called CurrentPageTypeName. This Depedency prop is part of this custom Tab Control. public static DependencyProperty CurrentPageTypeNameProperty = DependencyProperty.Register("CurrentPageTypeName", typeof(object), typeof(TabControlView)); public object CurrentPageTypeName { get { return GetValue(CurrentPageTypeNameProperty) as object; } set { SetValue(CurrentPageTypeNameProperty, value); } } In another view, where I am using the custom TabControl I then bind my property, with the actual name value, to CurrentPageTypeName property as seen below: <Views:TabControlView Grid.Row="0" Name="RunPageTabControl" TabItemsSource="{Binding RunPageTypeViewModels}" SelectedTab="{Binding Converter={StaticResource debugConverter}}" CurrentPageTypeName="{Binding Path=RunPageName, Mode=TwoWay}" TabContentTemplateSelector="{StaticResource tabItemTemplateSelector}" SelectedIndex="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SelectedTabIndex}" ShowPageType="Hidden" > <!--<Views:TabControlView.TabContentTemplate> <DataTemplate DataType="{x:Type ViewModels:RunPageTypeViewModel}"> <RunViews:RunPageTypeView/> </DataTemplate> </Views:TabControlView.TabContentTemplate>--> </Views:TabControlView> My problem is that nothing seems to be happening. It is grabbing its Content from the Itemsource, and not from my chained Dependency props. Is what I am trying even possible? If so, what have I done wrong. Thanks for looking.

    Read the article

  • Using LINQ, how do you get all label controls.

    - by John
    I want to get a collection of all label controls that are part of a user control. I have the following code: var labelControls = from Control ctl in this.Controls where ctl.GetType() == typeof(Label) select ctl; but the result is zero results. Please assist. Thanks.

    Read the article

  • Describing Types question

    - by user288245
    I have a bunch of types (eg. LargePlane, SmallPlane) that could be in this collection i've made, how do i print like LargePlane? I've tried like typeOf() and stuff but it doesn't work. Within like a toString()? So when i output the collection it states what type it is.

    Read the article

  • How to return a value based on the type of the generic T

    - by Blankman
    I have a method like: public T Get<T>(string key) { } Now say I want to return "hello" if the type is a string, and 110011 if it is type int. how can I do that? typeof(T) doesn't seem to work. I ideally want to do a switch statement, and return something based on the Type of the generic (string/int/long/etc). Is this possible?

    Read the article

  • Creating instance of interface in C#

    - by Max
    I'm working with MS Excel interop in C# and I don't understand how this particular line of code works: var excel = new Microsoft.Office.Interop.Excel.Application(); where Microsoft.Office.Interop.Excel.Application is an INTERFACE defined as: [Guid("000208D5-0000-0000-C000-000000000046")] [CoClass(typeof(ApplicationClass))] public interface Application : _Application, AppEvents_Event { } I'm thinking that some magic happens when the interface is decorated with a CoClass attribute, but still how is it possible that we can create an instance of an interface with a new keyword? Shouldn't it generate a compile time error?

    Read the article

  • Regular Expression not disappearing

    - by user2439019
    I have 3 phone fields and any one is required. SO i had a custom validation class to make any one of them is required. And i am calling those class as follows [RegularExpression(@"^\(?([0-9]{3})\)?. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered phone format is not valid. <br/> 10 digits are required.<br/> No spaces between digits. <br/> Numbers only.")] [AtLeastOneRequired("PhoneHome", "PhoneMobile", "PhoneOffice", ErrorMessage = "Please provide either of PhoneHome or PhoneMobile or phoneOffice. ")] public string PhoneHome { get; set; } [StringLength(11, MinimumLength = 10)] [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered phone format is not valid. <br/> 10 digits are required.<br/> No spaces between digits. <br/> Numbers only.")] [Display(Name = "Prompt_PhoneOffice", ResourceType = typeof(ResContactItems))] public string PhoneOffice { get; set; } [StringLength(11, MinimumLength = 10)] [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered phone format is not valid. <br/> 10 digits are required.<br/> No spaces between digits. <br/> Numbers only.")] [Display(Name = "Prompt_PhoneMobile", ResourceType = typeof(ResContactItems))] public string PhoneMobile { get; set; } The problem is with only "PhoneHome " field , if i enter incorrect format, it will display error message based ont he regular expression given .But the regular expression message is not disappearing wwhen we enter the correct format too.It will disappear only in f the field is empty.Other two fields are showing properly. Is this due to the custom class i am calling.? Please help me to sort out this issue Thanks, Vidya

    Read the article

  • NHibernate: How to-reconfigure mappings at runtime?

    - by George Mauer
    Let's get this out of the way first: I know that SessionFactory is immutable - I'm trying to change the Configuration at runtime and regenerate ISessionFactory. Specifically, I have a Customer mapped that will have some fields added to its dynamic-component node at runtime. I would like to do something like this var newSessionFactory = previousConfiguration .RemoveClassMapping(typeof(Customer)) .AddXmlString(newMappingForCustomer) .BuildSessionFactory(); However, I don't see any obvious way to remove a mapping, is there anything I can do short of regenerating the entire Configuration?

    Read the article

< Previous Page | 30 31 32 33 34 35 36 37 38 39 40 41  | Next Page >